2012-07-16 104 views
0

考慮下面的枚舉:解析字符串作爲枚舉

public enum TestType 
{ 
    Mil, 
    IEEE 
} 

如何可以解析如下因素串上述枚舉?

Military 888d Test中的TestType.IEEE

我的想法案件TestType.Mil 或者 IEEE 1394情況下是要檢查是否字符串匹配「軍用」或「IEEE」的第一個字母,然後我將其設置爲我想要的枚舉,但問題是有其他情況下不應該被解析!

+3

你能列出不應該被解析的情況嗎? – 2012-07-16 09:57:49

+0

例如'Miltiary 833d Spiral'我不想要這個解析 – 2012-07-16 10:09:45

+1

爲什麼這是無效的?因爲'米斯特'?或'833d'或'螺旋'? – 2012-07-16 10:12:43

回答

1

我你的情況瞭解是字符串可以以任何格式來。你可以像「軍事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 

重要的是用相關的正則表達式或測試填充規則對象。你如何將這些值傳遞給數組,真的取決於你...

0

試試這個var result = Enum.Parse(type, value);

+0

-1:這不會按照OP的規範工作(它只會將'Mil'解析爲'TestType.Mil',但肯定不是'Military 888d Test')。 – 2012-07-16 10:01:17

2

由我已經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#

+5

我會在頂部顯示它是另一個答案的副本。 – 2012-07-16 10:02:45

+0

@TimSchmelter - 我的答案副本只有我會把這個在頂部,如果你想..不問題..感謝poiting這個 – 2012-07-16 10:21:40

0

編輯

使用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及更高版本。

0

請嘗試這種方式

TestType testType; 
    Enum.TryParse<TestType>("IEEE", out testType); 

,並要比較兩個字符串,然後

bool result = testType.ToString() == "IEEE"; 
0

簡單的方法會爲你做它:

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"); 
    } 
}