如何使多个相似的属性调用一个通用属性

发布于 2024-12-04 18:33:43 字数 717 浏览 1 评论 0原文

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

我有一个如下定义的位字段:

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

现在我的类在位字段中的每一位都有一个属性。每个属性只是返回值或设置相应的标志。例如

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

,现在我有相当多的属性,我想通过调用具有要设置或获取的位数的通用属性来简化它们(最好只有一行)。

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

在VB.NET中有什么办法可以实现这一点吗?

I'm wondering if it's possible in VB.NET to make similar properties call one generic one?
A sentence doesn't explain it well so here's a code example.

I have a bit field defined like this:

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

Now my class has one property per bit in the bit field. Each property just returns the value or sets the corresponding flag. 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

Now I have quite a lot of properties and I would like to simplify them (ideally just one line) by calling a generic property with the bit number to Set or Get.

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

Is there any way to achieve this in VB.NET?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

拥抱没勇气 2024-12-11 18:33:43

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

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

You can make the enumeration a parameter in a private property:

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 

And make a public property wrapper:

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文