C#中Enum.Parse的通用版本
我经常想知道为什么C#还没有实现genericsEnum.Parse
可以说我有
enum MyEnum { Value1, Value2 } 从XML文件/数据库条目我想创build一个枚举。
 MyEnum val = (MyEnum)Enum.Parse(typeof(MyEnum), "value1", true); 
难道它不能被实现为类似的东西
 MyEnum cal = Enum.Parse<MyEnum>("value1"); 
这可能看起来像一个小问题,但似乎是一个被忽视的问题。
有什么想法吗?
它已经在.NET 4中实现;)看看这里 。
 MyEnum cal; if (!Enum.TryParse<MyEnum>("value1", out cal)) throw new Exception("value1 is not valid member of enumeration MyEnum"); 
这里的讨论还包含一些有趣的观点。
并在问题的理想语法:
 MyEnum cal = Toolkit.Parse<MyEnum>("value1"); 
  注意 :由于C#禁止添加静态扩展,所以必须在其他地方放置函数。 我使用一个包含所有这些有用位的静态Toolkit类: 
 /// <summary> /// Converts the string representation of the name or numeric value of one or // more enumerated constants to an equivalent enumerated object. /// </summary> /// <typeparam name="TEnum">An enumeration type.</typeparam> /// <param name="value">A string containing the name or value to convert.</param> /// <returns>An object of type TEnum whose value is represented by value</returns> /// <exception cref="System.ArgumentNullException">enumType or value is null.</exception> /// <exception cref=" System.ArgumentException"> enumType is not an System.Enum. -or- /// value is either an empty string or only contains white space.-or- /// value is a name, but not one of the named constants defined for the enumeration.</exception> /// <exception cref="System.OverflowException">value is outside the range of the underlying type of enumType.</exception> public static TEnum Parse<TEnum>(String value) where TEnum : struct { return (TEnum)Enum.Parse(typeof(TEnum), value); } 
 尽pipeC#不允许System.Enum限制,但它允许在.NET中使用,C#可以使用具有这种限制的types或方法。 请参阅Jon Skeet的Unconstrained Melody库,其中包含完全符合您需要的代码。 
 public class EnumHelper { public static T? TryParse<T>(string text) where T: struct { if (string.IsNullOrEmpty(text)) { return null; } T r; if (Enum.TryParse<T>(text, out r)) { return r; } return null; } } 
@ ian-boyd答案稍微修改后的版本,使用扩展方法来避免在调用中指定静态类名:
 MyEnum cal = "value1".Parse<MyEnum>(); /// <summary> /// Converts the string representation of the name or numeric value of one or // more enumerated constants to an equivalent enumerated object. /// </summary> /// <typeparam name="TEnum">An enumeration type.</typeparam> /// <returns>An object of type TEnum whose value is represented by value</returns> /// <exception cref="System.ArgumentNullException">enumType or value is null.</exception> /// <exception cref=" System.ArgumentException"> enumType is not an System.Enum. -or- /// value is either an empty string or only contains white space.-or- /// value is a name, but not one of the named constants defined for the enumeration.</exception> /// <exception cref="System.OverflowException">value is outside the range of the underlying type of enumType.</exception> public static TEnum Parse<TEnum>(this String value) where TEnum : struct { return (TEnum)Enum.Parse(typeof(TEnum), value); } 
虽然稍微调整了一些方法,试图build立类似于最初的build议:
 MyEnum cal = Enum.Parse<MyEnum>("value1"); 
在我看来,这个语法在C#中是不可能的,因为Enumtypes被视为不可空。
如果我们调用“Enum.TryParse”方法传递一个不相应于枚举项的值,Enum的默认值将会在“out”variables中返回。 这就是为什么我们需要首先testing“Enum.TryParse”结果,因为只需调用
 MyEnum cal; Enum.TryParse<MyEnum>("value1", out cal); 
并且检查“cal”值并不总是给出可靠的结果。