I got the Int16 value from the database and must convert it to an enum type. Unfortunately, this is done in a code layer that knows very little about objects, except that it can be assembled through reflection.
Thus, he finishes calling Convert.ChangeType , which fails with an invalid exception exception.
I found what I consider to be a smelly workaround, for example:
String name = Enum.GetName(destinationType, value); Object enumValue = Enum.Parse(destinationType, name, false);
Is there a better way, so I donโt have to navigate this String operation?
Here's a short but complete program that you can use if someone needs to experiment:
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); } }
enums c #
Lasse Vรฅgsรฆther Karlsen Feb 03 '09 at 13:28 2009-02-03 13:28
source share