如何使多个相似的属性调用一个通用属性
我想知道在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以将枚举作为私有属性中的参数:
并创建公共属性包装器:
You can make the enumeration a parameter in a private property:
And make a public property wrapper: