Title | Get a property's attribute value in VB .NET |
Description | This example shows how to get a property's attribute value in VB .NET. |
Keywords | attribute, VB.NET, property |
Categories | VB.NET |
|
|
The Employee class has two properties with the creative names Property1 and Property2. The Browsable attribute of the first is True and the value of the second is False.
|
|
Public Class Employee
<Browsable(True)> _
Public Property Property1() As String
Get
End Get
Set(ByVal Value As String)
End Set
End Property
<Browsable(False)> _
Public Property Property2() As String
Get
End Get
Set(ByVal Value As String)
End Set
End Property
End Class
|
|
When the form loads, the program uses TypeDescriptor.GetProperties to get property descriptors for the Employee class. It then uses the appropriate descriptor's Attributes collection to get a BrowsableAttribute object for the properties. This object provides properties that give its value.
|
|
Private Sub Form1_Load(...) Handles MyBase.Load
Dim txt As String
' Get property descriptors for the Employee class.
Dim property_descriptors As _
PropertyDescriptorCollection = _
TypeDescriptor.GetProperties(GetType(Employee))
' Get the attributes for Property1.
Dim attributes As AttributeCollection = _
property_descriptors("Property1").Attributes
' Get the BrowsableAttribute object.
Dim browsable_attribute As BrowsableAttribute
browsable_attribute = _
CType(attributes(GetType(BrowsableAttribute)), _
BrowsableAttribute)
txt = "Property1 is "
If Not browsable_attribute.Browsable Then txt &= "not "
txt &= "browsable" & vbCrLf
' Get the attributes for Property2.
attributes = _
property_descriptors("Property2").Attributes
' Get the BrowsableAttribute object.
browsable_attribute = _
CType(attributes(GetType(BrowsableAttribute)), _
BrowsableAttribute)
txt &= "Property2 is "
If Not browsable_attribute.Browsable Then txt &= "not "
txt &= "browsable"
txtResults.Text = txt
txtResults.Select(0, 0)
End Sub
|
|
|
|