I created a method which takes an enum and transforms it in a Dictionary where each int is associated with the name (as string) of the enum
// Define like this
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 cast but the application is meant to be a very cheap-and-dirty C#-enum to Javascript-enum converter).
This 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 whether I could create a list of enums to use for a series of calls to iterate my calls.
This was in the original question: [Why can’t I call this?]
What should I do to be able to create a call like this one?
List<Type> enumsToConvertList = new List<Type>();
enumsToConvertList.Add(typeof(_myEnum1));
enumsToConvertList.Add(typeof(_myEnum2));
// this'll be a loop
var a = getDictionaryFromEnum<enumsToConvertList.ElementAt(0)>();
You can’t specify generic argument type at runtime (well, without reflection). So, simply create non-generic method, which accepts argument of
Typetype:Usage: