考虑下面的枚举:解析字符串作为枚举
public enum TestType
{
Mil,
IEEE
}
如何可以解析如下因素串上述枚举?
Military 888d Test
中的TestType.IEEE
我的想法案件TestType.Mil
或者 IEEE 1394
情况下是要检查是否字符串匹配“军用”或“IEEE”的第一个字母,然后我将其设置为我想要的枚举,但问题是有其他情况下不应该被解析!
考虑下面的枚举:解析字符串作为枚举
public enum TestType
{
Mil,
IEEE
}
如何可以解析如下因素串上述枚举?
Military 888d Test
中的TestType.IEEE
我的想法案件TestType.Mil
或者 IEEE 1394
情况下是要检查是否字符串匹配“军用”或“IEEE”的第一个字母,然后我将其设置为我想要的枚举,但问题是有其他情况下不应该被解析!
我你的情况了解是字符串可以以任何格式来。你可以像“军事888d测试”,“米尔1234测试”,“Milit xyz SOmething”等字符串...
在这种情况下,一个简单的Enum.Parse是不会帮助。您需要决定Enum的每个值,您要允许哪些组合。考虑下面的代码...
public enum TestType
{
Unknown,
Mil,
IEEE
}
class TestEnumParseRule
{
public string[] AllowedPatterns { get; set; }
public TestType Result { get; set; }
}
private static TestType GetEnumType(string test, List<TestEnumParseRule> rules)
{
var result = TestType.Unknown;
var any = rules.FirstOrDefault((x => x.AllowedPatterns.Any(y => System.Text.RegularExpressions.Regex.IsMatch(test, y))));
if (any != null)
result = any.Result;
return result;
}
var objects = new List<TestEnumParseRule>
{
new TestEnumParseRule() {AllowedPatterns = new[] {"^Military \\d{3}\\w{1} [Test|Test2]+$"}, Result = TestType.Mil},
new TestEnumParseRule() {AllowedPatterns = new[] {"^IEEE \\d{3}\\w{1} [Test|Test2]+$"}, Result = TestType.IEEE}
};
var testString1 = "Military 888d Test";
var testString2 = "Miltiary 833d Spiral";
var result = GetEnumType(testString1, objects);
Console.WriteLine(result); // Mil
result = GetEnumType(testString2, objects);
Console.WriteLine(result); // Unknown
重要的是用相关的正则表达式或测试填充规则对象。你如何将这些值传递给数组,真的取决于你...
试试这个var result = Enum.Parse(type, value);
-1:这不会按照OP的规范工作(它只会将'Mil'解析为'TestType.Mil',但肯定不是'Military 888d Test')。 – 2012-07-16 10:01:17
由我已经answred:How to set string in Enum C#?
枚举不能是字符串,但您可以附加属性,比你可以阅读枚举值如下......... ...........
public enum TestType
{
[Description("Military 888d Test")]
Mil,
[Description("IEEE 1394")]
IEEE
}
public static string GetEnumDescription(Enum value)
{
FieldInfo fi = value.GetType().GetField(value.ToString());
DescriptionAttribute[] attributes =
(DescriptionAttribute[])fi.GetCustomAttributes(
typeof(DescriptionAttribute),
false);
if (attributes != null &&
attributes.Length > 0)
return attributes[0].Description;
else
return value.ToString();
}
这里是好文章,如果你想通过它:Associating Strings with enums in C#
我会在顶部显示它是另一个答案的副本。 – 2012-07-16 10:02:45
@TimSchmelter - 我的答案副本只有我会把这个在顶部,如果你想..不问题..感谢poiting这个 – 2012-07-16 10:21:40
编辑
使用Enum.GetNames
:如果您在使用.NET4或更高版本可以使用Enum.TryParse
foreach (var value in Enum.GetNames(typeof(TestType)))
{
// compare strings here
if(yourString.Contains(value))
{
// what you want to do
...
}
}
。而Enum.Parse
可用于.NET2及更高版本。
请尝试这种方式
TestType testType;
Enum.TryParse<TestType>("IEEE", out testType);
,并要比较两个字符串,然后
bool result = testType.ToString() == "IEEE";
简单的方法会为你做它:
public TestType GetTestType(string testTypeName)
{
switch(testTypeName)
{
case "Military 888d Test":
return TestType.Mil;
case "IEEE 1394":
return TestType.IEEE;
default:
throw new ArgumentException("testTypeName");
}
}
你能列出不应该被解析的情况吗? – 2012-07-16 09:57:49
例如'Miltiary 833d Spiral'我不想要这个解析 – 2012-07-16 10:09:45
为什么这是无效的?因为'米斯特'?或'833d'或'螺旋'? – 2012-07-16 10:12:43