How about:
//Tested on LINQPad
void Main()
{
var test = GetDictionary();
Console.WriteLine(test["London"]);
}
public static IDictionary GetDictionary()
{
Type type = typeof(T);
if (type.IsEnum)
{
var values = Enum.GetValues(type);
var result = new Dictionary();
foreach (var value in values)
{
result.Add(value.ToString(), (int)value);
}
return result;
}
else
{
throw new InvalidOperationException();
}
}
public enum City
{
London = 1,
Liverpool = 20,
Leeds = 25
}