|
I'm writing an update to a software package that takes binary data from an instrument and creates a spreadsheet. There is a column that list a reading mode that in the older version of the software outputted either "TC" or "QM". Now in the updated version I want it to write something more descriptive; that being either "Time Corrected" or "Quick Mode", but I still want to maintain backwards compatibility.
I thought the best way to do this would be to use custom attributes where the new description and old description would be saved and then based on the settings of the software the program could choose the proper description.
First I created a custom attribute class:
<AttributeUsage(AttributeTargets.Field, AllowMultiple:=True, Inherited:=False)>
Public Class OutputDescriptionAttribute
Inherits Attribute
Private _oldDescription As String
Private _newDescription As String
Public Sub New(oldDescription As String, newDescription As String)
Me._oldDescription = oldDescription
Me._newDescription = newDescription
End Sub
End Class
Then I created the enum and applied the attributes:
Public Enum ReadingModeType
<OutputDescription("TC", "TimeCorrected")> TimeCorrected
<OutputDescription("QM", "QuickMode")> QuickMode
End Enum
Lastly I copied a method off of Microsoft's website that is supposed to get the values from custom attributes:
Public Shared Sub GetDescription(t As Type)
Dim MyAttribute As OutputDescriptionAttribute =
CType(Attribute.GetCustomAttribute(t, GetType(OutputDescriptionAttribute)), OutputDescriptionAttribute)
If MyAttribute Is Nothing Then
Console.WriteLine("The attribute was not found.")
Else
End If
End Sub
I got this method from http://msdn.microsoft.com/en-us/library/71s1zwct.aspx[^]. I then called this method like so:
Private Sub MainWindow_Loaded(sender As Object, e As RoutedEventArgs) Handles Me.Loaded
Dim s As ReadingModeType = ReadingModeType.QuickMode
test.GetDescription(s.GetType)
End Sub
However when I step through the code in the GetDescription method, the MyAttribute object returns nothing. This is my first time working with custom attributes so I know I'm probably missing something very simple. Can anyone please advise me how I can retrieve the descriptions for the attribute applied to the specified enum value?
Thanks in advance.
|
|
|
|
|
You'd need to fill a FieldInfo from the field whose attributes you'd wish to query. I've included a sample to reference;
Module Module1
<AttributeUsage(AttributeTargets.Field, AllowMultiple:=True, Inherited:=False)>
Public Class OutputDescriptionAttribute
Inherits Attribute
Private _oldDescription As String
Private _newDescription As String
Public Sub New(ByVal oldDescription As String, ByVal newDescription As String)
Me._oldDescription = oldDescription
Me._newDescription = newDescription
End Sub
Public ReadOnly Property CurrentDescription As String
Get
Return _newDescription
End Get
End Property
End Class
Public Enum ReadingModeType
<OutputDescription("TC", "TimeCorrected")> TimeCorrected
<OutputDescription("QM", "QuickMode")> QuickMode
End Enum
Sub Main()
Dim TCFieldInfo As Reflection.FieldInfo = GetType(ReadingModeType).GetField("TimeCorrected")
Dim customAttributes As Object() = TCFieldInfo.GetCustomAttributes(inherit:=True)
For Each Attrib As Attribute In customAttributes
If Attrib.GetType() Is GetType(OutputDescriptionAttribute) Then
Dim outputDescriptionAttrib As OutputDescriptionAttribute =
CType(Attrib, OutputDescriptionAttribute)
Console.WriteLine(outputDescriptionAttrib.CurrentDescription)
End If
Next
Console.ReadLine()
End Sub
End Module
|
|
|
|