C#表驱动法+一点反射实现“得到指定位数随机不重复字符串”三种方式的封装

1.结构

一个public class GetMethods{...}

类中的变量:

int codeCount=4定义获取随机字符串的位数,默认4
int rep = 0方法体中一个自增的变量

类中的方法,三种获取方式:

string GetNum()

获得数字组合的字符串
string GetStr()获得字母组合的字符串
string GetStrAndNum()获得数字和字母混合组合的字符串

第二个类 public class GetString{...}

类中的变量:

enum Code{ NUM,STR,StrAndNum }定义枚举作为参数,选择获取方式

类中的方法:

static string GetRandomCode(Code codeType,int codeCount)(重载)获取字符串总方法,参数为获取方式与组合位数
static string GetRandomCode(Code codeType)(重载)获取字符串总方法,参数为获取方式,默认4位
string ActionInTable(Code code)表驱动的方法体,参数为获取方式
2.代码

1)一个public class GetMethods{...}

注:该类中的方法来自:http://www.cnblogs.com/xiachufeng/archive/2012/04/01/2429065.html

 1     public class GetMethods
 2     {
 3         public int codeCount=4;
 4         static int rep = 0;
 5 
 6         /// <summary>
 7         /// 产生随机codeCount位数字组合代码
 8         /// </summary>
 9         /// <param name="codeCount">组合位数</param>
10         public string GetNum()
11         {
12             string str = string.Empty;
13             long num2 = DateTime.Now.Ticks + rep;
14             rep++;
15             Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep)));
16             for (int i = 0; i < codeCount; i++)
17             {
18                 int num = random.Next();
19                 str = str + ((char)(0x30 + ((ushort)(num % 10)))).ToString();
20             }
21             return str;
22         }
23 
24         /// <summary>
25         /// 产生随机codeCount位字母组合代码
26         /// </summary>
27         /// <param name="codeCount">组合位数</param>
28         public string GetStr()
29         {
30             string str = string.Empty;
31             long num2 = DateTime.Now.Ticks + rep;
32             rep++;
33             Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep)));
34             for (int i = 0; i < codeCount; i++)
35             {
36                 int num = random.Next();
37                 str = str + ((char)(0x41 + ((ushort)(num % 0x1a)))).ToString();
38             }
39             return str;
40         }
41 
42         /// <summary>
43         /// 产生随机codeCount位数字与字母组合代码
44         /// </summary>
45         /// <param name="codeCount">组合位数</param>
46         public string GetStrAndNum()
47         {
48             string str = string.Empty;
49             long num2 = DateTime.Now.Ticks + rep;
50             rep++;
51             Random random = new Random(((int)(((ulong)num2) & 0xffffffffL)) | ((int)(num2 >> rep)));
52             for (int i = 0; i < codeCount; i++)
53             {
54                 char ch;
55                 int num = random.Next();
56                 if ((num % 2) == 0)
57                 {
58                     ch = (char)(0x30 + ((ushort)(num % 10)));
59                 }
60                 else
61                 {
62                     ch = (char)(0x41 + ((ushort)(num % 0x1a)));
63                 }
64                 str = str + ch.ToString();
65             }
66             return str;
67         }
68     }

2)第二个类 public class GetString{...}

 1     public class GetString
 2     {
 3         public enum Code{ NUM,STR,StrAndNum }
 4 
 5         public static string GetRandomCode(Code codeType,int codeCount)
 6         {
 7             GetMethods getMethod=new GetMethods();
 8             getMethod.codeCount = codeCount;
 9             var addMethod = typeof(GetMethods).GetMethod(ActionInTable(codeType));
10             return (string)addMethod.Invoke(getMethod,null);
11         }
12 
13         //重载,默认4位
14         public static string GetRandomCode(Code codeType)
15         {
16             GetMethods getMethod=new GetMethods();
17             var addMethod = typeof(GetMethods).GetMethod(ActionInTable(codeType));
18             return (string)addMethod.Invoke(getMethod,null);
19         }
20         
21         static string ActionInTable(Code code)
22         {
23             string[] methods = { "GetNum", "GetStr", "GetStrAndNum" };
24             return methods[(int)code];
25         }
26     }
3.应用

将该两个类封装到类库中,即可方便使用,我将他们封装到了StringControl.dll中,将其添加到引用中,引用命名空间using StringControl;

using StringControl;//引用命名空间
///代码省略
//.....................
///代码省略
//使用方法

string RandomKey = GetString.GetRandomCode(GetString.Code.StrAndNum,8);
string RandomKey1 = GetString.GetRandomCode(GetString.Code.StrAndNum); //4位
///代码省略
//.....................
///代码省略
4.下载

地址:https://files.cnblogs.com/smlusm/StringControl.zip

5.总结

利用该方法,有时可以避免过长的if与switch语句,确实方便,有想法的同学,还望不吝赐教!