使用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>();
或者使用查询语法:
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);
或者,如果每个字段可能有多个属性,您可能会想要这样做:
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>();
或者使用查询语法:
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;
Where(v => (enumVar & v) != v)
子句正确吗?难道不应该是(enumVar & v) == v
或者(enumVar & v) != 0
吗?编辑:算了,让我去喝杯咖啡。第二次编辑:不,我想我是对的。天哪,我快疯了。 - Chris Sinclair!= 0
,但后来意识到== v
可能更好(但没有修复!=
),但实际上.HasFlag(v)
在我看来比两者都要好。我想我也需要喝咖啡:p - p.s.w.gFirstOrDefault()
代替索引器[0]
来处理可能没有此属性的标志。然后,我检查列表中的空值并做出适当的反应。 - martinoss