繁体   English   中英

将字符串解析为枚举类型

[英]Parse string to enum type

我有一个像这样的枚举类型作为例子:

public Enum MyEnum {
    enum1, enum2, enum3 };

我将从配置文件中读取一个字符串。 我需要的是将字符串解析为 MyEnum 类型或 null 或未定义。 不确定以下代码是否有效(抱歉现在无法访问我的 VS):

// example: ParseEnum<MyEnum>("ENUM1", ref eVal);
bool ParseEnum<T>(string value1, ref eVal) where T : Enum
{
  bool bRet = false;
  var x = from x in Enum.GetNames(typeof(T)) where 
       string.Equals(value1, x, StringComparison. OrdinalIgnoreCase)
       select x;
  if (x.Count() == 1 )
  {
    eVal = Enum.Parse(typeof(T), x.Item(0)) as T;
    bRet = true;
  }
  return bRet;
}

不确定它是否正确或有任何其他简单的方法可以将字符串解析为 MyEnum 值?

怎么样:

public static class EnumUtils
{
    public static Nullable<T> Parse<T>(string input) where T : struct
    {
        //since we cant do a generic type constraint
        if (!typeof(T).IsEnum)
        {
            throw new ArgumentException("Generic Type 'T' must be an Enum");
        }
        if (!string.IsNullOrEmpty(input))
        {
            if (Enum.GetNames(typeof(T)).Any(
                  e => e.Trim().ToUpperInvariant() == input.Trim().ToUpperInvariant()))
            {
                return (T)Enum.Parse(typeof(T), input, true);
            }
        }
        return null;
    }
}

用作:

MyEnum? value = EnumUtils.Parse<MyEnum>("foo");

(注意:旧版本在Enum.Parse周围使用try/catch

private enum MyEnum
{
    Enum1 = 1, Enum2 = 2, Enum3 = 3, Enum4 = 4, Enum5 = 5, Enum6 = 6, 
    Enum7 = 7, Enum8 = 8, Enum9 = 9, Enum10 = 10
}

private static Object ParseEnum<T>(string s)
{
    try
    {
        var o = Enum.Parse(typeof (T), s);
        return (T)o;
    }
    catch(ArgumentException)
    {
        return null;
    }
}

static void Main(string[] args)
{
   Console.WriteLine(ParseEnum<MyEnum>("Enum11"));
   Console.WriteLine(ParseEnum<MyEnum>("Enum1"));
   Console.WriteLine(ParseEnum<MyEnum>("Enum6").GetType());
   Console.WriteLine(ParseEnum<MyEnum>("Enum10"));
}

输出:

    //This line is empty as Enum11 is not there and function returns a null
Enum1
TestApp.Program+MyEnum
Enum10
Press any key to continue . . .

这是一个老问题,但现在 .NET 4.5 有Enum.TryParse<TEnum>()

如果您想避免使用 try/catch,您可以使用TryParse

MyEnum eVal;
if (Enum.TryParse("ENUM2", true, out eVal)){
    // now eVal is the enumeration element: enum2 
}
//unable to parse. You can log the error, exit, redirect, etc...

我稍微修改了选定的答案。 我希望你喜欢它。

public static class EnumUtils
{
    public static Nullable<T> Parse<T>(string input) where T : struct
    {
        //since we cant do a generic type constraint
        if (!typeof(T).IsEnum)
        {
            throw new ArgumentException("Generic Type 'T' must be an Enum");
        }

        int intVal;
        if (!string.IsNullOrEmpty(input) && !int.TryParse(input, out intVal))
        {
            T eVal;
            if (Enum.TryParse(input, true, out eVal))
            {
                return eVal;
            }
        }
        return null;
    }
}

如果您使用的是 .NET 3.5(甚至 2.0,如果您去掉扩展方法),我很幸运地使用了本文中的技术:

枚举和字符串 - 停止疯狂!

编辑:域消失了,现在是一个链接农场。 我在工作时从我们的代码库中提取了代码(稍作修改并随着时间的推移添加),您现在可以在这里找到:

https://gist.github.com/1305566

我在UnconstrainedMelody 中有一个TryParseName方法,这是一个用于委托和枚举实用程序方法的库,它通过一些后期构建技巧使用“无法表达”的约束。 使用库的代码不需要后期构建,只是为了清楚。)

你会像这样使用它:

Foo foo;
bool parsed = Enums.TryParseName<Foo>(name, out foo);

我目前没有不区分大小写的版本,但如果您愿意,我可以轻松地介绍一个。 请注意,这不会像内置版本那样尝试解析数字,例如“12”,也不会尝试解析逗号分隔的标志列表。 稍后我可能会添加标志版本,但我在数字版本中看不到太多意义。

这是在没有装箱和执行时间类型检查的情况下完成的。 有约束真的很方便:)

如果您发现不区分大小写的解析有用,请告诉我...

我刚刚从合并的语法在这里,从异常处理在这里,创造这样的:

public static class Enum<T>
{
    public static T Parse(string value)
    {
        //Null check
        if(value == null) throw new ArgumentNullException("value");
        //Empty string check
        value = value.Trim();
        if(value.Length == 0) throw new ArgumentException("Must specify valid information for parsing in the string", "value");
        //Not enum check
        Type t = typeof(T);
        if(!t.IsEnum) throw new ArgumentException("Type provided must be an Enum", "TEnum");

        return (T)Enum.Parse(typeof(T), value);
    }
}

您可以稍微调整一下以返回 null 而不是抛出异常。

要按字符串返回 Enum,如果包含:

    public static T GetEnum<T>(string s)
    {
        Array arr = Enum.GetValues(typeof(T));
        foreach (var x in arr)
        {
            if (x.ToString().Contains(s))
                return (T)x;
        }
        return default(T);
    }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM