我有这个枚举:
<Flags()>
Public Enum FilterEnum As Integer
Green= 0
Blue = 1
Red = 2
Yellow = 4
End Enum
我想给&#34; Green&#34;和&#34;黄&#34;某种属性所以当我得到这样的枚举时:
Dim enumItems = [Enum].GetValues(myEnum)
我只会获得具有该属性的人的Enum值,如下所示:
Dim enumItems = [Enum].GetValues(myEnum).where(function(o) o.myAttribute)
答案 0 :(得分:2)
您可以通过以下方式创建自定义属性:
<AttributeUsage(AttributeTargets.Field)>
Public Class SomeAttribute
Inherits System.Attribute
Public Property SomeValue As String
End Class
然后使用您的属性创建您的枚举并装饰字段:
Public Enum MyEnum
<Some(SomeValue:="Good One")>
Member1 = 1
<Some(SomeValue:="Bad One")>
Member2 = 2
<Some(SomeValue:="Good One")>
Member3 = 3
End Enum
使用此查询,获取您想要的内容,例如&#34; Good One&#34; s
'Indented to be more readable step by step
Dim result As List(Of MyEnum) = _
GetType(MyEnum).GetFields() _
.Where(Function(field) _
field.GetCustomAttributes(True) _
.Cast(Of SomeAttribute) _
.Any(Function(attribute) attribute.SomeValue = "Good One")) _
.Select(Function(filtered) _
CType(filtered.GetValue(Nothing), MyEnum)) _
.ToList()
结果将是:
答案 1 :(得分:1)
也许你可以使用System.ComponentModel.Description
<Flags()>
Public Enum FilterEnum As Integer
<System.ComponentModel.Description("value_1")>_
Green= 0
<System.ComponentModel.Description("value_2")>_
Blue = 1
<System.ComponentModel.Description("value_3")>_
Red = 2
<System.ComponentModel.Description("value_4")>_
Yellow = 4
End Enum