Consider the enum below:
public enum TestType
{
Mil,
IEEE
}
How can I parse the folowing strings to the above enum?
Military 888d Test
in case of TestType.Mil
Or IEEE 1394
in case of TestType.IEEE
My idea was to check if the first letters of string matches 'Mil' or 'IEEE' then I would set it as the enum I want, but the problemis there are other cases that should not be parsed!
Already answred by me : How to set string in Enum C#?
Enum cannot be string but you can attach attribute and than you can read the value of enum as below....................
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();
}
here is good article if you want to go through it : Associating Strings with enums in C#
My understanding of your situation is that the string can come in any format. You could have strings like "Military 888d Test", "Mil 1234 Test", "Milit xyz SOmething"...
In such a scenario, a simple Enum.Parse is NOT going to help. You will need to decide for each value of the Enum, which combinations do you want to allow. Consider the code below...
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
The important thing is populating that rules object with the relevant regular expressions or tests. How you get those values in to the array, really depends on you...
试试这个var result = Enum.Parse(type, value);
EDIT
use Enum.GetNames
:
foreach (var value in Enum.GetNames(typeof(TestType)))
{
// compare strings here
if(yourString.Contains(value))
{
// what you want to do
...
}
}
If you using .NET4 or later you can use Enum.TryParse
. and Enum.Parse
is available for .NET2 and later.
Please try this way
TestType testType;
Enum.TryParse<TestType>("IEEE", out testType);
and it you want to compare string then
bool result = testType.ToString() == "IEEE";
Simple method will do it for you:
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");
}
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.