使用Key =枚举名称和值=枚举值创建一个键值对列表,使用最佳技术 [英] Create a list of Key Value pairs using Key = Enums Name and Value = Enums value using best technique
问题描述
在C#4.0 - 5.0中,如何创建一个键值对的列表,其中的键是枚举名称的值,该值是枚举名称值。
In C# 4.0 - 5.0 how do you create a list of key value pairs where the key is the enums name's value and the value is the enums name value.
另外在C#中如何创建一个键值对的列表,其中的关键字是枚举名称,值是属性EnumName名称。
Also in C# how do you create a list of key value pairs where the key is the enums name and the value is the attributes EnumName name.
我想要有两个单独的列表基于枚举的名称,以便从attribtue中检索值或人类可读取的名称。
I want to have two seperate list based on the enum's name in order to retrieve the value or the human readable name from the attribtue.
示例:
public enum QATypes
{
[EnumMember(Value = "Black Box")]
BlackBox = 10,
[EnumMember(Value = "Integration Tests")]
IntegrationTests = 20,
[EnumMember(Value = "Acceptance Testing")]
AcceptanceTesting= 30,
...
}
,第一个列表将如下所示: / p>
and the first list would look like this for values:
{{ key:"BlackBox", value:10 }, { key:"IntegrationTests ", value:20 },{ key:"AcceptanceTesting", value:30 },...}
,第二个列表将如下所示枚举:
and the second list would look like this for enumsmember:
{{ key:"BlackBox", value:"Black Box"}, { key:"IntegrationTests ", value:"Integration Tests"},{ key:"AcceptanceTesting", value:"Acceptance Testing"},...}
推荐答案
Enum.GetValues
是危险的,如:
enum { A, B = A, C }
您应该使用 Enum.GetNames
,然后获取相应的值。
You should use Enum.GetNames
and then get the corresponding values.
我有这个 helper 类来处理类似的事情:
I have this helper class to deal with similar things:
public static class Enum<T> where T : struct, IComparable, IFormattable, IConvertible
{
public static IEnumerable<T> GetValues()
{
return (T[])Enum.GetValues(typeof(T));
}
public static IEnumerable<string> GetNames()
{
return Enum.GetNames(typeof(T));
}
public static T Parse(string @enum)
{
return (T)Enum.Parse(typeof(T), @enum);
}
public static S GetAttribute<S>(string @enum) where S : Attribute
{
return (S)typeof(T).GetMember(@enum)[0]
.GetCustomAttributes(typeof(S), false)
.SingleOrDefault();
}
}
这是完全通用的,所以你需要更多打字来打电话。现在我可以做:
This is fully generic, so you need a bit more typing to call. Now I can do:
var first = Enum<QATypes>.GetNames().ToDictionary(x => x, x => (int)Enum<QATypes>.Parse(x));
var second = first.ToDictionary(x => x.Key, x => Enum<QATypes>.GetAttribute<EnumMemberAttribute>(x.Key).Value);
您可以在不需要传递枚举属性类型参数的合适命名空间中创建自己的扩展方法,以缓解通话。
You can create your own extension method in a suitable namespace where you need not pass the enum attribute type argument, to ease calling.
此外,您可以使助手类非静态,这将有助于您更好地使用类型推理,从而使调用代码缩短。应该看起来像:
Also you can make the helper class non-static which will help you with type-inference better and thus make calling code shorter. Should look like:
var helper = new Enum<QATypes>();
var first = helper.GetNames().ToDictionary(x => x, x => (int)helper.Parse(x));
var second = first.ToDictionary(x => x.Key, x => helper.GetAttribute<EnumMemberAttribute>(x.Key).Value);
这篇关于使用Key =枚举名称和值=枚举值创建一个键值对列表,使用最佳技术的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!