Tag Archives: Enumeration

Convert Enum to Dictionary: Another Silly Method

So what if you want the names and values from an Enum, but wanted them in dictionary form. Well shoot, a little bit of linq and little bit of that and you got this: public static IDictionary<String, Int32> ConvertEnumToDictionary<K>() { if (typeof(K).BaseType != typeof(Enum)) { throw new InvalidCastException(); } return Enum.GetValues(typeof(K)).Cast<Int32>().ToDictionary(currentItem => Enum.GetName(typeof(K), currentItem)); } [...]

1 Comment Continue Reading →

Random Enumeration Generator with Generics

public static I RandomEnumeration<I>() { I enumerationToCheck; Int32 indexToUse; String[] names; //Use activator to create an instance of the type I enumerationToCheck = System.Activator.CreateInstance<I>(); //Make sure the instance is an Enumeration //Unfortunately you can’t check that in the method //delcaring using “which”. if (enumerationToCheck as Enum == null) { throw new InvalidOperationException(); } //Get the [...]

Leave a comment Continue Reading →