如何使几个类似的属性调用一个通用的属性

时间:2011-09-14 22:27:16

标签: vb.net properties

我想知道在VB.NET中是否有可能使类似的属性调用一个通用的属性? 一句话不能很好地解释,所以这是一个代码示例。

我有一个像这样定义的字段:

<Flags()> _
Enum E_Operation As Integer
    Upload = 1
    Download = 2
    Overwrite = 4
    etc...
End Enum

现在我的类在位字段中每位有一个属性。每个属性只返回值或设置相应的标志。 e.g。

Public Property IsUpload() As Boolean
Get
    Return ((Operation And E_Operation.Upload) = E_Operation.Upload)
End Get
Set(ByVal value As Boolean)
    SetBit(E_Operation.Upload, value)
End Set
End Property

现在我有很多属性,我希望通过调用具有Set或Get位数的通用属性来简化它们(理想情况下只需一行)。

Public Property IsUpload() As Boolean
    GenericProperty(E_Operation.Upload)
End Property

有没有办法在VB.NET中实现这一点?

1 个答案:

答案 0 :(得分:2)

您可以将枚举作为私有属性中的参数:

Private Property OperationFlag(Flag As E_Operation) As Boolean 
    Get 
        Return ((Operation And Flag) = Flag) 
    End Get 
    Set(ByVal value As Boolean) 
        Operation = (Operation And Not Flag) Or (value And Flag)
    End Set 
End Property 

制作公共财产包装:

Public Property IsUpload As Boolean
    Get
        Return OperationFlag(E_Operation.Upload)
    End Get
    Set(value As Boolean)
        OperationFlag(E_Operation.Upload) = value
    End Set
End Property