Questions tagged «changetype»

3
Convert.ChangeType并转换为枚举?
我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 = …
70 c#  enums  changetype 
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.