您可以创建这样一个自定义属性:
<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
并使用此查询,得到你想要的,考试PLE “好一个” 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()
而结果将是:
'[枚举] .GetValues()'工作在一个类型,所以它应该是'[枚举。 GetValues(GetType(FilterEnum))'否则它就像你可能想要的那种声音'.HasFlag' – Plutonix