我Int16
从数据库中获得了一个值,需要将其转换为枚举类型.不幸的是,这是在一个代码层中完成的,这些代码对于对象知之甚少,除了它可以通过反射收集的内容.
因此,它最终调用Convert.ChangeType
失败,并出现无效的强制转换异常.
我找到了我认为有臭味的解决方法,如下所示:
String name = Enum.GetName(destinationType, value); Object enumValue = Enum.Parse(destinationType, name, false);
有没有更好的方法,所以我不必通过这个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); } }
Peter.. 87
Enum.ToObject(....
是你在找什么!
C#
StringComparison enumValue = (StringComparison)Enum.ToObject(typeof(StringComparison), 5);
VB.NET
Dim enumValue As StringComparison = CType([Enum].ToObject(GetType(StringComparison), 5), StringComparison)
如果你做了很多枚举转换尝试使用下面的类,它将为你节省很多代码.
public class Enumwhere EnumType : struct, IConvertible { /// /// Retrieves an array of the values of the constants in a specified enumeration. /// ////// public static EnumType[] GetValues() { return (EnumType[])Enum.GetValues(typeof(EnumType)); } /// /// Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. /// /// ////// public static EnumType Parse(string name) { return (EnumType)Enum.Parse(typeof(EnumType), name); } /// /// Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. /// /// /// ////// public static EnumType Parse(string name, bool ignoreCase) { return (EnumType)Enum.Parse(typeof(EnumType), name, ignoreCase); } /// /// Converts the specified object with an integer value to an enumeration member. /// /// ////// public static EnumType ToObject(object value) { return (EnumType)Enum.ToObject(typeof(EnumType), value); } }
现在不用写,(StringComparison)Enum.ToObject(typeof(StringComparison), 5);
你可以简单地写Enum
.
Enum.ToObject(....
是你在找什么!
C#
StringComparison enumValue = (StringComparison)Enum.ToObject(typeof(StringComparison), 5);
VB.NET
Dim enumValue As StringComparison = CType([Enum].ToObject(GetType(StringComparison), 5), StringComparison)
如果你做了很多枚举转换尝试使用下面的类,它将为你节省很多代码.
public class Enumwhere EnumType : struct, IConvertible { /// /// Retrieves an array of the values of the constants in a specified enumeration. /// ////// public static EnumType[] GetValues() { return (EnumType[])Enum.GetValues(typeof(EnumType)); } /// /// Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. /// /// ////// public static EnumType Parse(string name) { return (EnumType)Enum.Parse(typeof(EnumType), name); } /// /// Converts the string representation of the name or numeric value of one or more enumerated constants to an equivalent enumerated object. /// /// /// ////// public static EnumType Parse(string name, bool ignoreCase) { return (EnumType)Enum.Parse(typeof(EnumType), name, ignoreCase); } /// /// Converts the specified object with an integer value to an enumeration member. /// /// ////// public static EnumType ToObject(object value) { return (EnumType)Enum.ToObject(typeof(EnumType), value); } }
现在不用写,(StringComparison)Enum.ToObject(typeof(StringComparison), 5);
你可以简单地写Enum
.