有没有人有更优雅的解析枚举的解决方案?以下对我来说似乎是一团糟。
UserType userType = (UserType)Enum.Parse(typeof(UserType), iUserType.ToString());
我经常为它做一个通用的助手:
public static T ParseEnum<T>(string value) where T:struct
{
return (T)Enum.Parse(typeof(T), value);
}
您可以将其与Jon Skeet 的 Unstrained Melody(或任何其他 IL 后处理器)结合使用,以获得对枚举的正确类型约束,但这是可选的。
然后你可以像这样使用它:
var enumValue = ParseEnum<UserType>(iUserType.ToString());
.NET Framework 4.0 也Enum.TryParse
提供了类似的语法,并提供了一种在解析失败时进行处理的方法。例如:
UserType userType;
if (Enum.TryParse<UserType>(iUserType.ToString(), out userType))
{
//Yay! Parse succeeded. The userType variable has the value.
}
else
{
//Oh noes! The parse failed!
}
您可以像这样创建扩展方法
public static class EnumExtensions
{
public static T ToEnum<T>(this string s)
{
return (T)Enum.Parse(typeof(T), s);
}
}
然后在代码中你可以这样使用它(MyEnum 包含值 A 和 B):
string s = "B";
MyEnum e = s.ToEnum<MyEnum>();
这是基于@vcsjones 版本和反馈以及来自@Mones 示例的扩展方法:
public enum TestEnum
{
None,
A,
B,
};
void Main()
{
var testValues = new List<object>();
testValues.AddRange(Enumerable.Range(-2, 6).Select(i => (object)i));
testValues.AddRange(new List<string>() { String.Empty, "A", "B", "C", null });
foreach (var testValue in testValues)
{
Console.WriteLine($"Testing value {testValue ?? String.Empty}:");
TestEnum output;
var enumValues = Enum.GetNames(typeof(TestEnum)).ToList();
try
{
if (TestEnum.TryParse(testValue.ToString(), out output))
{
Console.WriteLine($"Succeeded with TryParse on {testValue} to {output}");
}
else
{
Console.WriteLine($"Failed to TryParse on {testValue}");
}
}
catch (Exception ex)
{
Console.WriteLine($"Test harness caught an exception: {ex.ToString()}");
}
var toEnumOutput = (testValue ?? String.Empty).ToString().Parse<TestEnum>();
Console.WriteLine($"Parse<TEnum> returned {toEnumOutput}");
Console.WriteLine();
Console.WriteLine();
}
}
public static class EnumExtensions
{
public static TEnum Parse<TEnum>(this string value) where TEnum : struct
{
TEnum output = default(TEnum);
var enumValues = Enum.GetNames(typeof(TEnum)).ToList();
if (Enum.TryParse<TEnum>(value, true, out output))
if (Enum.IsDefined(typeof(TEnum), value) || value.ToString().Contains(",") || enumValues.Contains(output.ToString()))
{
Console.WriteLine($"Converted '{value}' to {output}.");
return output;
}
else
{
Console.WriteLine($"{value} is not an underlying value of the enumeration.");
}
else
{
Console.WriteLine($"{value} is not a member of the enumeration.");
}
return default(TEnum);
}
}
测试工具给出以下输出:
Testing value -2:
Succeeded with TryParse on -2 to -2
-2 is not an underlying value of the enumeration.
Parse<TEnum> returned None
Testing value -1:
Succeeded with TryParse on -1 to -1
-1 is not an underlying value of the enumeration.
Parse<TEnum> returned None
Testing value 0:
Succeeded with TryParse on 0 to None
Converted '0' to None.
Parse<TEnum> returned None
Testing value 1:
Succeeded with TryParse on 1 to A
Converted '1' to A.
Parse<TEnum> returned A
Testing value 2:
Succeeded with TryParse on 2 to B
Converted '2' to B.
Parse<TEnum> returned B
Testing value 3:
Succeeded with TryParse on 3 to 3
3 is not an underlying value of the enumeration.
Parse<TEnum> returned None
Testing value :
Failed to TryParse on
is not a member of the enumeration.
Parse<TEnum> returned None
Testing value A:
Succeeded with TryParse on A to A
Converted 'A' to A.
Parse<TEnum> returned A
Testing value B:
Succeeded with TryParse on B to B
Converted 'B' to B.
Parse<TEnum> returned B
Testing value C:
Failed to TryParse on C
C is not a member of the enumeration.
Parse<TEnum> returned None
Testing value :
Test harness caught an exception: System.NullReferenceException: Object reference not set to an instance of an object.
at UserQuery.Main() in C:\Users\Colin\AppData\Local\Temp\3\LINQPad5\_zhvrhwll\query_ludjga.cs:line 49
is not a member of the enumeration.
Parse<TEnum> returned None
参考:
哦,我遇到了 Tyler Brinkley 的Enums.NET库,它可以做到这一点,甚至更多!
另一个 StackOverflow 答案C# 中 Enum.Parse 的通用版本导致 Jon Skeet 的Unconstrained Melody库站点,该站点将我们引导到 Enums.NET。哇。