我有以下枚举:
public enum Brands
{
HP = 1,
IBM = 2,
Lenovo = 3
}
从中我想制作一个格式的字典:
// key = name + "_" + id
// value = name
var brands = new Dictionary<string, string>();
brands[HP_1] = "HP",
brands[IBM_2] = "IBM",
brands[Lenovo_3] = "Lenovo"
到目前为止,我已经这样做了,但是很难从该方法创建字典:
public static IDictionary<string, string> GetValueNameDict<TEnum>()
where TEnum : struct, IConvertible, IComparable, IFormattable
{
if (!typeof(TEnum).IsEnum)
throw new ArgumentException("TEnum must be an Enumeration type");
var res = from e in Enum.GetValues(typeof (TEnum)).Cast<TEnum>()
select // couldn't do this
return res;
}
谢谢!