如果我有枚举字符串或枚举int值,我怎么能得到枚举值.例如:如果我有一个枚举如下:
public enum TestEnum
{
Value1 = 1,
Value2 = 2,
Value3 = 3
}
Run Code Online (Sandbox Code Playgroud)
在一些字符串变量中,我的值为"value1",如下所示:
string str = "Value1"
Run Code Online (Sandbox Code Playgroud)
或者在某个int变量中,我的值为2
int a = 2;
Run Code Online (Sandbox Code Playgroud)
我如何获得枚举的实例?我想要一个泛型方法,我可以提供枚举和我的输入字符串或int值来获取枚举实例.
Ken*_*rey 137
不,您不需要通用方法.这更容易:
MyEnum myEnum = (MyEnum)myInt;
MyEnum myEnum = (MyEnum)Enum.Parse(typeof(MyEnum), myString);
Run Code Online (Sandbox Code Playgroud)
我认为它也会更快.
小智 22
有很多方法可以做到这一点,但是如果你想要一个简单的例子,那就可以了.它只需要通过必要的防御性编码来增强,以检查类型安全性和无效解析等.
/// <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>();
}
Run Code Online (Sandbox Code Playgroud)
Sri*_*vel 13
如果使用TryParseor 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);
}
}
Run Code Online (Sandbox Code Playgroud)
正如@chrfin在评论中所指出的那样,只需this在参数类型之前添加就可以非常方便地使其成为一种扩展方法.
小智 13
正如@Phil B在@Kendall Frey的答案的评论中所建议的,对于字符串来说,这可以非常简洁地完成。
Enum.Parse<MyEnum>(myString);
Run Code Online (Sandbox Code Playgroud)
添加此内容是因为@Kendall Frey的答案给了我一个运行时错误,而@Phil B的修订则没有。
以下是C#中通过字符串获取枚举值的方法
///
/// 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;
}
Run Code Online (Sandbox Code Playgroud)
以下是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;
}
Run Code Online (Sandbox Code Playgroud)
如果我有一个枚举如下:
public enum TestEnum
{
Value1 = 1,
Value2 = 2,
Value3 = 3
}
Run Code Online (Sandbox Code Playgroud)
那么我可以使用上述方法作为
TestEnum reqValue = GetEnumValue<TestEnum>("Value1"); // Output: Value1
TestEnum reqValue2 = GetEnumValue<TestEnum>(2); // OutPut: Value2
Run Code Online (Sandbox Code Playgroud)
希望这会有所帮助。
| 归档时间: |
|
| 查看次数: |
150754 次 |
| 最近记录: |