Nullable(Of):<attribute>的参数类型。它有用吗?</attribute>

时间:2012-06-05 12:19:25

标签: vb.net reflection attributes

我正在尝试将类型布尔值用于某些参数。

<AttributeUsage(AttributeTargets.Method, Inherited:=True, AllowMultiple:=False)>
Public Class MyAttribute
             Inherits Attribute
  Property MyCondition As Boolean
End Class

我遇到的问题是,在我的对象 MyAttribute 中,我无法说出布尔属性的值是否为False:

  • 因为存在参数(e:MyCondition:= false)

或,

  • 因为没有传递参数,并且由于我的对象初始化,我的属性值为False。

我虽然使用了属性Nullable(Of Boolean),但似乎不允许这样做?

Property MyCondition As Nullable(of Boolean)

错误消息属性或字段'MyCondition'没有有效的属性类型。”

这种情况有没有绕行?唯一可行的参数类型是String(在实例化时为null)?

3 个答案:

答案 0 :(得分:2)

我相信你只能使用可以在代码中表示为文字常量的简单数据类型(例如100,True,“Test”)。这可能就是Nul​​lable(Of Boolean)不适合你的原因。但是,您可以通过创建实际的属性处理程序来完成您要执行的操作:

<AttributeUsage(AttributeTargets.Method, Inherited:=True, AllowMultiple:=False)>
Public Class MyAttribute
    Inherits Attribute

    Public Property MyCondition As Boolean
        Get
            Return _myCondition
        End Get
        Set(ByVal value As Boolean)
            _myCondition = value
            _explicitlySet = True
        End Set
    End Property
    Private _myCondition As Boolean = False

    Public ReadOnly Property ExplicitlySet As Boolean
        Get
            Return _explicitlySet
        End Get
    End Property
    Private _explicitlySet As Boolean = False
End Class

或者,你可以使它成为只读的可空属性并使用构造函数来设置它:

<AttributeUsage(AttributeTargets.Method, Inherited:=True, AllowMultiple:=False)>
Public Class MyAttribute
    Inherits Attribute

    Public Sub New()
    End Sub

    Public Sub New(myCondition As Boolean)
        _myCondition = myCondition
    End Sub

    Public ReadOnly Property MyCondition As Nullable(Of Boolean)
        Get
            Return _myCondition
        End Get
    End Property
    Private _myCondition As Nullable(Of Boolean)
End Class

答案 1 :(得分:0)

使用枚举

public enum TriState
{
    NotSet,
    False,
    True
}

答案 2 :(得分:0)

我会使用三类方法

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()
相关问题