C# 转换枚举为字典
FieldInfo[] fields = typeof(EnumToDictionaryEnum)
.GetFields(BindingFlags.Static | BindingFlags.Public)
?? Array.Empty();
var dictionary = fields.ToDictionary(k => k.Name, v => (int)v.GetValue(null));
var dictionary2 = Enum.GetValues(typeof(EnumToDictionaryEnum))
.Cast()
.ToDictionary(k => k.ToString(), v => (int)v);
示例代码
EnumToDictionaryTestDemo