如何为Enum值TryParse?

Man*_*ani 92 c# enums

我想编写一个函数,它可以验证给定值(作为字符串传递)对可能的值enum.在匹配的情况下,它应该返回枚举实例; 否则,它应该返回一个默认值.

该函数可能不在内部使用try/ catch,它排除使用Enum.Parse,在给定无效参数时抛出异常.

我想使用TryParse函数的一些东西来实现这个:

public static TEnum ToEnum<TEnum>(this string strEnumValue, TEnum defaultValue)
{
   object enumValue;
   if (!TryParse (typeof (TEnum), strEnumValue, out enumValue))
   {
       return defaultValue;
   }
   return (TEnum) enumValue;
}
Run Code Online (Sandbox Code Playgroud)

Tho*_*rin 102

Enum.IsDefined将完成任务.它可能不如TryParse那样有效,但它可以在没有异常处理的情况下工作.

public static TEnum ToEnum<TEnum>(this string strEnumValue, TEnum defaultValue)
{
    if (!Enum.IsDefined(typeof(TEnum), strEnumValue))
        return defaultValue;

    return (TEnum)Enum.Parse(typeof(TEnum), strEnumValue);
}
Run Code Online (Sandbox Code Playgroud)

值得注意的是:TryParse.NET 4.0中添加了一种方法.

  • Enum.IsDefined的缺点:http://blogs.msdn.com/brada/archive/2003/11/29/50903.aspx (13认同)
  • IsDefined也没有忽略的情况 (6认同)
  • @Anthony:如果你想支持不区分大小写,你需要`GetNames`.在内部,所有这些方法(包括`Parse`)使用`GetHashEntry`,它执行实际反射 - 一次.从好的方面来看,.NET 4.0有一个TryParse,它也是通用的:) (2认同)

小智 31

正如其他人所说,你必须实现自己的TryParse.Simon Mourier正在提供一个完整的实现,它可以处理所有事情.

如果您正在使用位域枚举(即标志),您还必须处理一个字符串"MyEnum.Val1|MyEnum.Val2",其中包含两个枚举值的组合.如果您只是Enum.IsDefined使用此字符串调用,它将返回false,即使Enum.Parse正确处理它.

更新

正如Lisa和Christian在评论中所提到的,Enum.TryParse现在可用于.NET4及更高版本的C#.

MSDN文档


Sim*_*ier 20

这是一个自定义的实现EnumTryParse.与其他常见实现不同,它还支持使用Flags属性标记的枚举.

    /// <summary>
    /// Converts the string representation of an enum to its Enum equivalent value. A return value indicates whether the operation succeeded.
    /// This method does not rely on Enum.Parse and therefore will never raise any first or second chance exception.
    /// </summary>
    /// <param name="type">The enum target type. May not be null.</param>
    /// <param name="input">The input text. May be null.</param>
    /// <param name="value">When this method returns, contains Enum equivalent value to the enum contained in input, if the conversion succeeded.</param>
    /// <returns>
    /// true if s was converted successfully; otherwise, false.
    /// </returns>
    public static bool EnumTryParse(Type type, string input, out object value)
    {
        if (type == null)
            throw new ArgumentNullException("type");

        if (!type.IsEnum)
            throw new ArgumentException(null, "type");

        if (input == null)
        {
            value = Activator.CreateInstance(type);
            return false;
        }

        input = input.Trim();
        if (input.Length == 0)
        {
            value = Activator.CreateInstance(type);
            return false;
        }

        string[] names = Enum.GetNames(type);
        if (names.Length == 0)
        {
            value = Activator.CreateInstance(type);
            return false;
        }

        Type underlyingType = Enum.GetUnderlyingType(type);
        Array values = Enum.GetValues(type);
        // some enums like System.CodeDom.MemberAttributes *are* flags but are not declared with Flags...
        if ((!type.IsDefined(typeof(FlagsAttribute), true)) && (input.IndexOfAny(_enumSeperators) < 0))
            return EnumToObject(type, underlyingType, names, values, input, out value);

        // multi value enum
        string[] tokens = input.Split(_enumSeperators, StringSplitOptions.RemoveEmptyEntries);
        if (tokens.Length == 0)
        {
            value = Activator.CreateInstance(type);
            return false;
        }

        ulong ul = 0;
        foreach (string tok in tokens)
        {
            string token = tok.Trim(); // NOTE: we don't consider empty tokens as errors
            if (token.Length == 0)
                continue;

            object tokenValue;
            if (!EnumToObject(type, underlyingType, names, values, token, out tokenValue))
            {
                value = Activator.CreateInstance(type);
                return false;
            }

            ulong tokenUl;
            switch (Convert.GetTypeCode(tokenValue))
            {
                case TypeCode.Int16:
                case TypeCode.Int32:
                case TypeCode.Int64:
                case TypeCode.SByte:
                    tokenUl = (ulong)Convert.ToInt64(tokenValue, CultureInfo.InvariantCulture);
                    break;

                //case TypeCode.Byte:
                //case TypeCode.UInt16:
                //case TypeCode.UInt32:
                //case TypeCode.UInt64:
                default:
                    tokenUl = Convert.ToUInt64(tokenValue, CultureInfo.InvariantCulture);
                    break;
            }

            ul |= tokenUl;
        }
        value = Enum.ToObject(type, ul);
        return true;
    }

    private static char[] _enumSeperators = new char[] { ',', ';', '+', '|', ' ' };

    private static object EnumToObject(Type underlyingType, string input)
    {
        if (underlyingType == typeof(int))
        {
            int s;
            if (int.TryParse(input, out s))
                return s;
        }

        if (underlyingType == typeof(uint))
        {
            uint s;
            if (uint.TryParse(input, out s))
                return s;
        }

        if (underlyingType == typeof(ulong))
        {
            ulong s;
            if (ulong.TryParse(input, out s))
                return s;
        }

        if (underlyingType == typeof(long))
        {
            long s;
            if (long.TryParse(input, out s))
                return s;
        }

        if (underlyingType == typeof(short))
        {
            short s;
            if (short.TryParse(input, out s))
                return s;
        }

        if (underlyingType == typeof(ushort))
        {
            ushort s;
            if (ushort.TryParse(input, out s))
                return s;
        }

        if (underlyingType == typeof(byte))
        {
            byte s;
            if (byte.TryParse(input, out s))
                return s;
        }

        if (underlyingType == typeof(sbyte))
        {
            sbyte s;
            if (sbyte.TryParse(input, out s))
                return s;
        }

        return null;
    }

    private static bool EnumToObject(Type type, Type underlyingType, string[] names, Array values, string input, out object value)
    {
        for (int i = 0; i < names.Length; i++)
        {
            if (string.Compare(names[i], input, StringComparison.OrdinalIgnoreCase) == 0)
            {
                value = values.GetValue(i);
                return true;
            }
        }

        if ((char.IsDigit(input[0]) || (input[0] == '-')) || (input[0] == '+'))
        {
            object obj = EnumToObject(underlyingType, input);
            if (obj == null)
            {
                value = Activator.CreateInstance(type);
                return false;
            }
            value = obj;
            return true;
        }

        value = Activator.CreateInstance(type);
        return false;
    }
Run Code Online (Sandbox Code Playgroud)

  • 如下所述,但并不真正可见:从 .Net 4 开始,Enum.TryParse 可用并且无需额外编码即可工作。更多信息可从 MSDN 获得:http://msdn.microsoft.com/library/vstudio/dd991317%28v=vs.100%29.aspx (2认同)

Ric*_*ard 16

最后你必须实现这个Enum.GetNames:

public bool TryParseEnum<T>(string str, bool caseSensitive, out T value) where T : struct {
    // Can't make this a type constraint...
    if (!typeof(T).IsEnum) {
        throw new ArgumentException("Type parameter must be an enum");
    }
    var names = Enum.GetNames(typeof(T));
    value = (Enum.GetValues(typeof(T)) as T[])[0];  // For want of a better default
    foreach (var name in names) {
        if (String.Equals(name, str, caseSensitive ? StringComparison.Ordinal : StringComparison.OrdinalIgnoreCase)) {
            value = (T)Enum.Parse(typeof(T), name);
            return true;
        }
    }
    return false;
}
Run Code Online (Sandbox Code Playgroud)

补充说明:

  • Enum.TryParse包含在.NET 4中.请参阅此处http://msdn.microsoft.com/library/dd991876(VS.100).aspx
  • 另一种方法是直接包装Enum.Parse捕获失败时抛出的异常.找到匹配项时可能会更快,但如果没有,则可能会更慢.根据您正在处理的数据,这可能是也可能不是净改进.

编辑:刚刚看到一个更好的实现,它缓存了必要的信息:http://damieng.com/blog/2010/10/17/enums-better-syntax-improved-performance-and-tryparse-in-net- 3-5


Hug*_*rio 8

基于.NET 4.5

示例代码如下

using System;

enum Importance
{
    None,
    Low,
    Medium,
    Critical
}

class Program
{
    static void Main()
    {
    // The input value.
    string value = "Medium";

    // An unitialized variable.
    Importance importance;

    // Call Enum.TryParse method.
    if (Enum.TryParse(value, out importance))
    {
        // We now have an enum type.
        Console.WriteLine(importance == Importance.Medium);
    }
    }
}
Run Code Online (Sandbox Code Playgroud)

参考:http://www.dotnetperls.com/enum-parse


Eve*_*ael 5

enum EnumStatus
{
    NAO_INFORMADO = 0,
    ENCONTRADO = 1,
    BLOQUEADA_PELO_ENTREGADOR = 2,
    DISPOSITIVO_DESABILITADO = 3,
    ERRO_INTERNO = 4,
    AGARDANDO = 5
}
Run Code Online (Sandbox Code Playgroud)

...

if (Enum.TryParse<EnumStatus>(item.status, out status)) {

}
Run Code Online (Sandbox Code Playgroud)