C#知识拾遗

 参数验证方式

1.    一般方法

1.1 手动验证

最为普遍常见,略。

1.2 使用扩展方法验证

在C#3.0 中,引入了扩展方法,可以以一种更优雅的方式来进行参数验证,如:

 1 //参数辅助类
 2 public static class ArgumentHelper
 3 {
 4     public static void RequireRange(this int value, int minValue, int maxValue, string argumentName)
 5     {
 6         if (value > maxValue || value < minValue)
 7         {
 8             throw new ArgumentException(string.Format("The value must be between {0} and {1}", minValue, maxValue), argumentName);
 9         }
10     }
11 
12     public static void RequireNotNullOrEmpty(this string value, string argumentName)
13     {
14         if (string.IsNullOrEmpty(value))
15         {
16             throw new ArgumentException("The value can't be null or empty", argumentName);
17         }
18     }
19 }
20 
21 //用户注册
22 public bool Register(string name, int age)
23 {
24    name.RequireNotNullOrEmpty("name");
25    age.RequireRange(10,70,"age");
26     //insert into db
27 }
View Code

2. 使用类型或框架

使用类库

FluentValidationCuttingEdge.ConditionsEnterprise Liberary(Validation Application Block组件),Code Contracts(.NET 4.0正式引用)等。

2.2 使用框架

         如ASP.NET MVC的Model中数据标记(Data Annotations) 属性。

参考资料:C# 中参数验证方式的演变

 

全角与半角字符转换

全角空格为12288,半角空格为32,其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248。基于此便不难实现他们之间的转换:

 1         /// <summary>
 2         ///转全角的函数(SBC case)
 3         ///全角空格为12288,半角空格为32
 4         ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
 5         /// </summary>
 6         /// <param name="input">输入</param>
 7         /// <returns></returns>
 8         public static String ToSBC(String input)
 9         {
10             char[] c = input.ToCharArray();
11 
12             for (int i = 0; i < c.Length; i++)
13             {
14                 if (c[i] == 32)
15                 {
16                     c[i] = (char)12288;
17 
18                     continue;
19                 }
20 
21                 if (c[i] < 127)
22 
23                     c[i] = (char)(c[i] + 65248);
24             }
25 
26             return new String(c);
27         }
28 
29 
30         /// <summary>
31         ///转半角的函数(DBC case)
32         ///全角空格为12288,半角空格为32
33         ///其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248
34         /// </summary>
35         /// <param name="input"></param>
36         /// <returns></returns>
37         public static String ToDBC(String input)
38         {
39             char[] c = input.ToCharArray();
40 
41             for (int i = 0; i < c.Length; i++)
42             {
43                 if (c[i] == 12288)
44                 {
45                     c[i] = (char)32;
46                     continue;
47                 }
48 
49                 if (c[i] > 65280 && c[i] < 65375)
50 
51                     c[i] = (char)(c[i] - 65248);
52             }
53 
54             return new String(c);
55         }
View Code

参考资料:C#全角和半角转换

转string时,null的处理:

Convert.ToString能处理字符串为null的情况。ToString方法不能处理字符串为null的情况,会抛出异常。

string/byte[]/MemoryStream/Base64String的相互转换

关系如下图:

1.字符串转比特数组

(1)byte[] bt=System.Text.Encoding.Default.GetBytes("字符串");

(2)byte[] bt=Convert.FromBase64String("字符串");

2.字符串转流

(1)MemoryStream ms=new MemoryStream(System.Text.Encoding.Default.GetBytes("字符串"));

(2)MemoryStream ms=new MemoryStream(Convert.FromBase64String("字符串"));

3.流转比特数组

(1)byte[] bt=ms.ToArray();

(2)MemoryStream ms=new MemoryStream();ms.Write(bt,0,ms.Length);

4.流转字符串

(1)string str=Convert.ToBase64String(ms.ToArray());

(2)string str=System.Text.Encoding.Default.GetString(ms.ToArray());

5.比特数组转字符串

(1)string str=System.Text.Encoding.Default.GetString(bt);

(2)string str=Convert.ToBase64String(bt);

6.比特数组转流

(1)MemoryStream ms=new MemoryStream(bt);

(2)MemoryStream ms=new MemoryStream();ms.Read(bt,0,bt.Lenght);

参考资料:字符串string 、byte[]、MemoryStream、Base64String的相互转换

c# 反射Invoke调用方法获得out带出的值

         输出参数在最后,基于些获取方法如下:

1             var msg = string.Empty;
2             object[] parameters = new object[] { model, null };
3 
4             //函数签名为public bool Update(DB_BedInfo model, out string mess);
5             updateMethod.Invoke(serviceObj, parameters);
6 
7             //parameters[1]即为out带出的值
8             msg = Convert.ToString(parameters[1]);

具名参数和可选参数

  具名参数 和 可选参数 是 C#  4.0 出来的新特性。当有多个可选参数,前面的可选参数不想传时,可使用具名参数解决。

猜你喜欢

转载自www.cnblogs.com/yscit/p/10338865.html