如果我有枚举字符串或枚举int值,如何获取枚举值。例如:如果我有一个枚举如下:
public enum TestEnum
{
Value1 = 1,
Value2 = 2,
Value3 = 3
}
在某些字符串变量中,我的值为“value1”,如下所示:
string str = "Value1"
或在某个int变量中,我的值为2,如
int a = 2;
如何获取枚举实例?我想要一个泛型方法,我可以提供枚举和我的输入字符串或int值来获取枚举实例。
答案 0 :(得分:114)
不,您不需要通用方法。这更容易:
MyEnum enum = (MyEnum)myInt;
MyEnum enum = (MyEnum)Enum.Parse(typeof(TestEnum), myString);
我认为它也会更快。
答案 1 :(得分:20)
有很多方法可以做到这一点,但如果你想要一个简单的例子,那就可以了。它只需要通过必要的防御性编码来增强,以检查类型安全性和无效解析等。
/// <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>();
}
答案 2 :(得分:13)
如果您使用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在评论中所指出的那样,只需在参数类型之前添加this
就可以非常方便地使其成为一种扩展方法。
答案 3 :(得分:4)
以下是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;
}
以下是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
希望这会有所帮助。
答案 4 :(得分:2)
我认为您忘记了泛型类型定义:
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>();
答案 5 :(得分:2)
从SQL数据库获取枚举,如:
SqlDataReader dr = selectCmd.ExecuteReader();
while (dr.Read()) {
EnumType et = (EnumType)Enum.Parse(typeof(EnumType), dr.GetString(0));
....
}
答案 6 :(得分:1)
只需尝试这个
这是另一种方式
public enum CaseOriginCode
{
Web = 0,
Email = 1,
Telefoon = 2
}
public void setCaseOriginCode(string CaseOriginCode)
{
int caseOriginCode = (int)(CaseOriginCode)Enum.Parse(typeof(CaseOriginCode), CaseOriginCode);
}
这是对您的文章的评论
答案 7 :(得分:0)
尝试这样的事情
public static TestEnum GetMyEnum(this string title)
{
EnumBookType st;
Enum.TryParse(title, out st);
return st;
}
所以你可以做到
TestEnum en = "Value1".GetMyEnum();
答案 8 :(得分:0)
这里是获取字符串/值的示例
public enum Suit
{
Spades = 0x10,
Hearts = 0x11,
Clubs = 0x12,
Diamonds = 0x13
}
private void print_suit()
{
foreach (var _suit in Enum.GetValues(typeof(Suit)))
{
int suitValue = (byte)(Suit)Enum.Parse(typeof(Suit), _suit.ToString());
MessageBox.Show(_suit.ToString() + " value is 0x" + suitValue.ToString("X2"));
}
}
Result of Message Boxes
Spade value is 0x10
Hearts value is 0x11
Clubs value is 0x12
Diamonds value is 0x13
答案 9 :(得分:0)
您可以使用以下方法进行操作:
combi_sum <- dcast(merge(mtcarsTOTAL[,.(cost, gear)], iris[, .N, .(carb, gear, gender, age)], by = "gear"),
gender + age ~ carb, value.var = "cost", fun.aggregate = sum, fill = 0)
structure(list(gender = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), age = c(1L, 2L,
3L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 11L, 12L, 13L, 14L), `1` = c(978, 978, 0, 0, 1074, 0, 0,
0, 2642, 2642, 0, 0, 0, 0, 3620, 0, 978, 2642, 0, 0, 978, 0,
0, 978, 2052, 0, 0, 0, 0, 1074, 978, 0, 0, 0, 3620, 0), `2` = c(0,
0, 0, 978, 0, 0, 0, 2052, 0, 2642, 978, 0, 0, 0, 0, 0, 0, 0,
1074, 2642, 0, 0, 0, 0, 0, 0, 0, 1074, 0, 978, 0, 2642, 0, 0,
978, 2642), `3` = c(0, 0, 0, 0, 0, 978, 2642, 0, 2642, 0, 0,
2642, 978, 0, 978, 2642, 0, 0, 0, 0, 1074, 3620, 2642, 0, 0,
0, 978, 0, 2642, 0, 0, 2642, 0, 2642, 0, 0), `4` = c(0, 0, 1074,
0, 0, 0, 978, 0, 0, 0, 1074, 1074, 0, 2052, 0, 0, 0, 0, 0, 1074,
0, 2642, 1074, 978, 978, 2642, 0, 0, 2642, 0, 2052, 0, 2642,
1074, 0, 0)), row.names = c(NA, -36L), class = c("data.table",
"data.frame"), .internal.selfref = <pointer: 0x7fb24d802ee0>, sorted = c("gender",
"age"))
combi_length <- dcast(merge(mtcarsTOTAL[,.(cost, gear)], iris[, .N, .(carb, gear, gender, age)], by = "gear"),
gender + age ~ carb, value.var = "cost", fun.aggregate = length, fill = 0)
structure(list(gender = c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), age = c(1L, 2L,
3L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 1L, 3L, 4L, 5L, 6L, 7L, 8L,
9L, 11L, 12L, 13L, 14L), `1` = c(1L, 1L, 0L, 0L, 1L, 0L, 0L,
0L, 1L, 1L, 0L, 0L, 0L, 0L, 2L, 0L, 1L, 1L, 0L, 0L, 1L, 0L, 0L,
1L, 2L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 0L, 0L, 2L, 0L), `2` = c(0L,
0L, 0L, 1L, 0L, 0L, 0L, 2L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L,
0L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 0L, 1L, 0L, 1L, 0L,
0L, 1L, 1L), `3` = c(0L, 0L, 0L, 0L, 0L, 1L, 1L, 0L, 1L, 0L,
0L, 1L, 1L, 0L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 2L, 1L, 0L, 0L, 0L,
1L, 0L, 1L, 0L, 0L, 1L, 0L, 1L, 0L, 0L), `4` = c(0L, 0L, 1L,
0L, 0L, 0L, 1L, 0L, 0L, 0L, 1L, 1L, 0L, 2L, 0L, 0L, 0L, 0L, 0L,
1L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 0L, 2L, 0L, 1L, 1L, 0L,
0L)), row.names = c(NA, -36L), class = c("data.table", "data.frame"
), .internal.selfref = <pointer: 0x7fb24d802ee0>, sorted = c("gender",
"age"))
注意:此方法仅是示例,您可以对其进行改进。