2

我正在尝试将 Boolean 类型用于某些参数。

<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' 没有有效的属性类型。”

有这种情况吗?唯一可行的参数类型是字符串(实例化时为空)吗?

4

3 回答 3

2

我相信您只能使用可以在代码中表示为文字常量的简单数据类型(例如 100、True、“Test”)。这可能就是 Nullable(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
于 2012-06-05T12:30:08.750 回答
0

改为使用枚举

public enum TriState
{
    NotSet,
    False,
    True
}
于 2012-06-05T12:30:43.490 回答
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()
于 2012-06-05T13:32:54.627 回答