从数值.NET标志枚举获取属性 [英] .NET Flag Enum get Attributes from values
本文介绍了从数值.NET标志枚举获取属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
问候计算器,
如果我有与标志枚举类型的属性,以及与自己的属性该枚举类型的值,我怎么能检索所有的相应属性?
例如:
[标志()]
枚举MyEnum
{
[EnumDisplayName(枚举值1)]
EnumValue1 = 1,
[EnumDisplayName(枚举值2)]
EnumValue2 = 2,
[EnumDisplayName(枚举值3)
EnumValue3 = 4,
}
无效美孚()
{
VAR enumVar = MyEnum.EnumValue2 | MyEnum.EnumValue3;
//获取EnumDisplayName集合从enumVar属性对象
...
}
解决方案
使用LINQ一个快速和肮脏的方式:
的IEnumerable< EnumDisplayNameAttribute>属性=
Enum.GetValues(typeof运算(MyEnum))
.Cast< MyEnum>()
。凡(V => enumVar.HasFlag(v))中
。选择(V =>的typeof(MyEnum).GetField(v.ToString()))
。选择(F => f.GetCustomAttributes(typeof运算(EnumDisplayNameAttribute),假)[0])
.Cast&其中; EnumDisplayNameAttribute>();
或者在查询语法:
的IEnumerable< EnumDisplayNameAttribute>属性=
从Enum.GetValues MyEnum V(typeof运算(MyEnum))
其中enumVar.HasFlag(v)的
令f = typeof运算(MyEnum).GetField(v.ToString())
让= f.GetCustomAttributes(typeof运算(EnumDisplayNameAttribute),假)[0]
选择((EnumDisplayNameAttribute)一);
另外,如果有可能可能是对每个字段多个属性,你可能会想这样做:
的IEnumerable< EnumDisplayNameAttribute>属性=
Enum.GetValues(typeof运算(MyEnum))
.Cast< MyEnum>()
。凡(V => enumVar.HasFlag(v))中
。选择(V =>的typeof(MyEnum).GetField(v.ToString()))
.SelectMany(F => f.GetCustomAttributes(typeof运算(EnumDisplayNameAttribute),FALSE))
.Cast&其中; EnumDisplayNameAttribute>();
或者在查询语法:
的IEnumerable< EnumDisplayNameAttribute>属性=
从Enum.GetValues MyEnum V(typeof运算(MyEnum))
其中enumVar.HasFlag(v))中
令f = typeof运算(MyEnum).GetField(v.ToString())
从EnumDisplayNameAttribute一个在f.GetCustomAttributes(typeof运算(EnumDisplayNameAttribute),FALSE)
选择;
Greetings StackOverflow,
If I've got an enum type with the Flag attribute as well as the values in this enum type with their own attributes, how can I retrieve all of the appropriate attributes?
For example:
[Flags()]
enum MyEnum
{
[EnumDisplayName("Enum Value 1")]
EnumValue1 = 1,
[EnumDisplayName("Enum Value 2")]
EnumValue2 = 2,
[EnumDisplayName("Enum Value 3")]
EnumValue3 = 4,
}
void Foo()
{
var enumVar = MyEnum.EnumValue2 | MyEnum.EnumValue3;
// get a collection of EnumDisplayName attribute objects from enumVar
...
}
解决方案
A quick and dirty way using Linq:
IEnumerable<EnumDisplayNameAttribute> attributes =
Enum.GetValues(typeof(MyEnum))
.Cast<MyEnum>()
.Where(v => enumVar.HasFlag(v))
.Select(v => typeof(MyEnum).GetField(v.ToString()))
.Select(f => f.GetCustomAttributes(typeof(EnumDisplayNameAttribute), false)[0])
.Cast<EnumDisplayNameAttribute>();
Or in query syntax:
IEnumerable<EnumDisplayNameAttribute> attributes =
from MyEnum v in Enum.GetValues(typeof(MyEnum))
where enumVar.HasFlag(v)
let f = typeof(MyEnum).GetField(v.ToString())
let a = f.GetCustomAttributes(typeof(EnumDisplayNameAttribute), false)[0]
select ((EnumDisplayNameAttribute)a);
Alternatively, if there could possibly be multiple attributes on each field, you'll probably want to do this:
IEnumerable<EnumDisplayNameAttribute> attributes =
Enum.GetValues(typeof(MyEnum))
.Cast<MyEnum>()
.Where(v => enumVar.HasFlag(v))
.Select(v => typeof(MyEnum).GetField(v.ToString()))
.SelectMany(f => f.GetCustomAttributes(typeof(EnumDisplayNameAttribute), false))
.Cast<EnumDisplayNameAttribute>();
Or in query syntax:
IEnumerable<EnumDisplayNameAttribute> attributes =
from MyEnum v in Enum.GetValues(typeof(MyEnum))
where enumVar.HasFlag(v))
let f = typeof(MyEnum).GetField(v.ToString())
from EnumDisplayNameAttribute a in f.GetCustomAttributes(typeof(EnumDisplayNameAttribute), false)
select a;
这篇关于从数值.NET标志枚举获取属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文