我会使用三类方法
MyAttribute (inherits Attribute)
ValidMyAttribute (interits MyAttribute)
InvalidMyAttribute (inherits MyAttribute)
' means true
<ValidMyAttribute> _
Public Sub MyMethod1()
' means false
<InvalidMyAttribute> _
Public Sub MyMethod2()
' means not defined
<MyAttribute> _
Public Sub MyMethod3()
类应该是这样的
Public class MyAttribute
Inherits Attribute
Public Overridable ReadOnly Property Value as Boolean?
Get
return nothing
End Get
End Property
End Class
Public class ValidMyAttribute
Inherits MyAttribute
Public Overrides ReadOnly Property Value as Boolean?
Get
return true
End Get
End Property
End Class
Public class ValidMyAttribute
Inherits MyAttribute
Public Overrides ReadOnly Property Value as Boolean?
Get
return false
End Get
End Property
End Class
更新:
只是再想一想,这应该容易得多:
<AttributeUsage(AttributeTargets.Method, Inherited:=True, AllowMultiple:=False)>
Public Class MyAttribute
Inherits Attribute
Public Property Value As Boolean? = Nothing
Public Sub New(value As Boolean)
Me.Value = value
End Sub
Public Sub New()
End Sub
End Class
用法:
<MyAttribute(True)> _
Public Sub Method()
<MyAttribute(false)> _
Public Sub Method()
' Value will be nothing
<MyAttribute()> _
Public Sub Method()