I created a method that takes an enumeration and converts it to a dictionary, where each int is associated with the name (as a string) of the enumeration
public static Dictionary<int, string> getDictionaryFromEnum<T>()
{
List<T> commandList = Enum.GetValues(typeof(T)).Cast<T>().ToList();
Dictionary<int, string> finalList = new Dictionary<int, string>();
foreach (T command in commandList)
{
finalList.Add((int)(object)command, command.ToString());
}
return finalList;
}
(ps. yes, I have a double click, but the application is for very cheap and dirty C # -enum for Javascript-enum converter).
It can be easily used like this.
private enum _myEnum1 { one = 1001, two = 1002 };
private enum _myEnum2 { one = 2001, two = 2002 };
var a = getDictionaryFromEnum<_myEnum1>();
var b = getDictionaryFromEnum<_myEnum2>();
Now I was wondering if I could create an enumeration list to use for a series of calls to iterate my calls.
That was in the original question: [Why can't I call it?]
What should I do to create a challenge like this?
List<Type> enumsToConvertList = new List<Type>();
enumsToConvertList.Add(typeof(_myEnum1));
enumsToConvertList.Add(typeof(_myEnum2));
var a = getDictionaryFromEnum<enumsToConvertList.ElementAt(0)>();
source
share