Convert.ChangeType并转换为枚举?

ang*_*son 64 c# enums changetype

Int16从数据库中获得了一个值,需要将其转换为枚举类型.不幸的是,这是在一个代码层中完成的,这些代码对于对象知之甚少,除了它可以通过反射收集的内容.

因此,它最终调用Convert.ChangeType失败,并出现无效的强制转换异常.

我找到了我认为有臭味的解决方法,如下所示:

String name = Enum.GetName(destinationType, value);
Object enumValue = Enum.Parse(destinationType, name, false);
Run Code Online (Sandbox Code Playgroud)

有没有更好的方法,所以我不必通过这个String操作?

这是一个简短但完整的程序,如果有人需要进行实验,可以使用它:

using System;

public class MyClass
{
    public enum DummyEnum
    {
        Value0,
        Value1
    }

    public static void Main()
    {
        Int16 value = 1;
        Type destinationType = typeof(DummyEnum);

        String name = Enum.GetName(destinationType, value);
        Object enumValue = Enum.Parse(destinationType, name, false);

        Console.WriteLine("" + value + " = " + enumValue);
    }
}
Run Code Online (Sandbox Code Playgroud)

Pet*_*ter 87

Enum.ToObject(.... 是你在找什么!

C#

StringComparison enumValue = (StringComparison)Enum.ToObject(typeof(StringComparison), 5);
Run Code Online (Sandbox Code Playgroud)

VB.NET

Dim enumValue As StringComparison = CType([Enum].ToObject(GetType(StringComparison), 5), StringComparison)
Run Code Online (Sandbox Code Playgroud)

如果你做了很多枚举转换尝试使用下面的类,它将为你节省很多代码.

public class Enum<EnumType> where EnumType : struct, IConvertible
{

    /// <summary>
    /// Retrieves an array of the values of the constants in a specified enumeration.
    /// </summary>
    /// <returns></returns>
    /// <remarks></remarks>
    public static EnumType[] GetValues()
    {
        return (EnumType[])Enum.GetValues(typeof(EnumType));
    }

    /// <summary>
    /// Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.
    /// </summary>
    /// <param name="name"></param>
    /// <returns></returns>
    /// <remarks></remarks>
    public static EnumType Parse(string name)
    {
        return (EnumType)Enum.Parse(typeof(EnumType), name);
    }

    /// <summary>
    /// Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object.
    /// </summary>
    /// <param name="name"></param>
    /// <param name="ignoreCase"></param>
    /// <returns></returns>
    /// <remarks></remarks>
    public static EnumType Parse(string name, bool ignoreCase)
    {
        return (EnumType)Enum.Parse(typeof(EnumType), name, ignoreCase);
    }

    /// <summary>
    /// Converts the specified object with an integer value to an enumeration member.
    /// </summary>
    /// <param name="value"></param>
    /// <returns></returns>
    /// <remarks></remarks>
    public static EnumType ToObject(object value)
    {
        return (EnumType)Enum.ToObject(typeof(EnumType), value);
    }
}
Run Code Online (Sandbox Code Playgroud)

现在不用写,(StringComparison)Enum.ToObject(typeof(StringComparison), 5);你可以简单地写Enum<StringComparison>.ToObject(5);.

  • 传说!不知道这个.不是stackoverflow布里尔?!:-) (3认同)