如何获得通过string或整数的枚举值
如何获得枚举值,如果我有枚举string或枚举int值。 例如:如果我有一个枚举如下:
public enum TestEnum { Value1 = 1, Value2 = 2, Value3 = 3 } 而在一些stringvariables中,我的值为“value1”,如下所示:
 string str = "Value1" 
或者在一些intvariables我有值2喜欢
 int a = 2; 
我怎么能得到枚举的实例? 我想要一个generics方法,我可以提供枚举和我的inputstring或int值来获取枚举实例。
不,你不需要一个通用的方法。 这很容易:
 MyEnum enum = (MyEnum)myInt; MyEnum enum = (MyEnum)Enum.Parse(typeof(TestEnum), myString); 
我认为这也会更快。
有很多方法可以做到这一点,但如果你想要一个简单的例子,这将是可行的。 只需要增强必要的防御性编码来检查types安全性和无效parsing等。
  /// <summary> /// Extension method to return an enum value of type T for the given string. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="value"></param> /// <returns></returns> public static T ToEnum<T>(this string value) { return (T) Enum.Parse(typeof(T), value, true); } /// <summary> /// Extension method to return an enum value of type T for the given int. /// </summary> /// <typeparam name="T"></typeparam> /// <param name="value"></param> /// <returns></returns> public static T ToEnum<T>(this int value) { var name = Enum.GetName(typeof(T), value); return name.ToEnum<T>(); } 
 如果您使用TryParse或Parse和ToObject方法可能会更简单。 
 public static class EnumHelper { public static T GetEnumValue<T>(string str) where T : struct, IConvertible { Type enumType = typeof(T); if (!enumType.IsEnum) { throw new Exception("T must be an Enumeration type."); } T val; return Enum.TryParse<T>(str, true, out val) ? val : default(T); } public static T GetEnumValue<T>(int intValue) where T : struct, IConvertible { Type enumType = typeof(T); if (!enumType.IsEnum) { throw new Exception("T must be an Enumeration type."); } return (T)Enum.ToObject(enumType, intValue); } } 
 正如@chrfin在注释中指出的那样,只要在参数types之前加上this参数,就可以非常容易地将它扩展为方便的。 
以下是C#中通过string获取枚举值的方法
  /// /// Method to get enumeration value from string value. /// /// /// public T GetEnumValue<T>(string str) where T : struct, IConvertible { if (!typeof(T).IsEnum) { throw new Exception("T must be an Enumeration type."); } T val = ((T[])Enum.GetValues(typeof(T)))[0]; if (!string.IsNullOrEmpty(str)) { foreach (T enumValue in (T[])Enum.GetValues(typeof(T))) { if (enumValue.ToString().ToUpper().Equals(str.ToUpper())) { val = enumValue; break; } } } return val; } 
以下是C#中通过int获取枚举值的方法。
  /// /// Method to get enumeration value from int value. /// /// /// public T GetEnumValue<T>(int intValue) where T : struct, IConvertible { if (!typeof(T).IsEnum) { throw new Exception("T must be an Enumeration type."); } T val = ((T[])Enum.GetValues(typeof(T)))[0]; foreach (T enumValue in (T[])Enum.GetValues(typeof(T))) { if (Convert.ToInt32(enumValue).Equals(intValue)) { val = enumValue; break; } } return val; } 
如果我有一个枚举如下:
 public enum TestEnum { Value1 = 1, Value2 = 2, Value3 = 3 } 
那么我可以利用上面的方法
 TestEnum reqValue = GetEnumValue<TestEnum>("Value1"); // Output: Value1 TestEnum reqValue2 = GetEnumValue<TestEnum>(2); // OutPut: Value2 
希望这会有所帮助。
我想你忘了genericstypes的定义:
 public T GetEnumValue<T>(int intValue) where T : struct, IConvertible // <T> added 
你可以改进它是最方便的,例如:
 public static T ToEnum<T>(this string enumValue) : where T : struct, IConvertible { return (T)Enum.Parse(typeof(T), enumValue); } 
那么你可以这样做:
 TestEnum reqValue = "Value1".ToEnum<TestEnum>(); 
只需尝试一下
这是另一种方式
 public enum CaseOriginCode { Web = 0, Email = 1, Telefoon = 2 } public void setCaseOriginCode(string CaseOriginCode) { int caseOriginCode = (int)(CaseOriginCode)Enum.Parse(typeof(CaseOriginCode), CaseOriginCode); } 
这是您的文章的评论
从SQL数据库获取枚举像:
 SqlDataReader dr = selectCmd.ExecuteReader(); while (dr.Read()) { EnumType et = (EnumType)Enum.Parse(typeof(EnumType), dr.GetString(0)); .... } 
尝试这样的事情
  public static TestEnum GetMyEnum(this string title) { EnumBookType st; Enum.TryParse(title, out st); return st; } 
所以你可以做
 TestEnum en = "Value1".GetMyEnum();