Parse分析之 -- Enum.Parse

原文地址:http://blog.csdn.net/aimeast/article/details/4516323

 

先看一个例子:

 

[c-sharp] view plaincopy
  1. enum MyEnum  
  2. {  
  3.     A = 1,  
  4.     B = 2,  
  5.     C = 3,  
  6. }  

 

 

执行下面代码

[c-sharp] view plaincopy
  1. MyEnum e = (MyEnum)Enum.Parse(typeof(MyEnum), "A,B");  


结果是:e == Myenum.C

 


为什么中间有个逗号还不会报错呢?

我不做过多的测试,直接拿出.Net Reflector,定位到

mscorlib/CommonLanguageRuntimeLibrary/System/Enum/Parse(Type, string) : Object

 

 

[c-sharp] view plaincopy
  1. [ComVisible(true)]  
  2. public static object Parse(Type enumType, string value)  
  3. {  
  4.     return Parse(enumType, value, false);  
  5. }  

 

 

 直接转到
public static object Parse(Type enumType, string value, bool ignoreCase);

下面就是我们要分析的代码

 

[c-sharp] view plaincopy
  1. [ComVisible(true)]  
  2. public static object Parse(Type enumType, string value, bool ignoreCase)  
  3. {  
  4.     if (enumType == null)  
  5.     {  
  6.         throw new ArgumentNullException("enumType");  
  7.     }  
  8.     if (!(enumType is RuntimeType))  
  9.     {  
  10.         throw new ArgumentException(Environment.GetResourceString("Arg_MustBeType"), "enumType");  
  11.     }  
  12.     if (!enumType.IsEnum)  
  13.     {  
  14.         throw new ArgumentException(Environment.GetResourceString("Arg_MustBeEnum"), "enumType");  
  15.     }  
  16.     if (value == null)  
  17.     {  
  18.         throw new ArgumentNullException("value");  
  19.     }  
  20.     value = value.Trim();  
  21.     if (value.Length == 0)  
  22.     {  
  23.         throw new ArgumentException(Environment.GetResourceString("Arg_MustContainEnumInfo"));  
  24.     }  
  25.     //前面一堆参数检测,不管他  
  26.     //直接从这里看起  
  27.     ulong num = 0L;  
  28.     if ((char.IsDigit(value[0]) || (value[0] == '-')) || (value[0] == '+'))//这里判断value是不是  
  29. 数字  
  30.     {  
  31.         Type underlyingType = GetUnderlyingType(enumType);  
  32.         try  
  33.         {  
  34.             //如果是数字,直接使用内部函数先获取类型,然后直接转换为相应的枚举类型,并返回结果  
  35.             object obj2 = Convert.ChangeType(value, underlyingType,   
  36. CultureInfo.InvariantCulture);  
  37.             return ToObject(enumType, obj2);  
  38.         }  
  39.         catch (FormatException)  
  40.         {  
  41.         }  
  42.     }  
  43.     //如果不是数字,先把字符串分割  
  44.     string[] strArray = value.Split(enumSeperatorCharArray);  
  45.     HashEntry hashEntry = GetHashEntry(enumType);//获取相应的HashEntry  
  46.     string[] names = hashEntry.names;  
  47.     //下面是两个for,第一个是枚举分割后的字符,第二个是枚举Enum类型  
  48.     for (int i = 0; i < strArray.Length; i++)  
  49.     {  
  50.         strArray[i] = strArray[i].Trim();  
  51.         bool flag = false;  
  52.         for (int j = 0; j < names.Length; j++)  
  53.         {  
  54.             ulong num4;  
  55.             if (ignoreCase)  
  56.             {  
  57.                 if (string.Compare(names[j], strArray[i], StringComparison.OrdinalIgnoreCase) ==   
  58. 0)  
  59.                 {  
  60.                     goto Label_0122;  
  61.                 }  
  62.                 continue;  
  63.             }  
  64.             if (!names[j].Equals(strArray[i]))  
  65.             {  
  66.                 continue;  
  67.             }  
  68.             //这里就是找到结果后的处理  
  69.         Label_0122:  
  70.             num4 = hashEntry.values[j];//获取当前的匹配的编号  
  71.             num |= num4;//与之前的编号进行按位或处理  
  72.             flag = true;  
  73.             break;  
  74.         }  
  75.         if (!flag)  
  76.         {  
  77.             throw new ArgumentException(string.Format(CultureInfo.CurrentCulture,   
  78. Environment.GetResourceString("Arg_EnumValueNotFound"), new object[] { value }));  
  79.         }  
  80.     }  
  81.     //返回这个编号的Enum值  
  82.     return ToObject(enumType, num);  
  83. }  

 

 


现在可以知道,出现这样的结果就是因为这句话的作用
num |= num4; 

把上面的测试稍作修改

 

 

[c-sharp] view plaincopy
  1. enum MyEnum  
  2. {  
  3.     A,  
  4.     B,  
  5.     C,  
  6. }  

 

 

执行下面代码
MyEnum e = (MyEnum)Enum.Parse(typeof(MyEnum), "A,B");
结果是:e == Myenum.B

为什么会得出B就不用我说了吧!

posted @ 2012-09-11 18:52  我思故我在...  阅读(929)  评论(0编辑  收藏  举报