Retrieving Custom Attributes on Enumeration Members
-
I designed a custom attribute using the following class:
_
Public Class OutputDescriptionAttribute
Inherits Attribute#Region "Variables"
Private _oldDescription As String
Private _newDescription As String
#End Region#Region "Properties"
Public ReadOnly Property OldDescription As String
Get
Return Me._oldDescription
End Get
End PropertyPublic ReadOnly Property NewDescription As String Get Return Me.\_newDescription End Get End Property
#End Region
#Region "Constructor"
Public Sub New(oldDescription As String, newDescription As String)
Me._oldDescription = oldDescription
Me._newDescription = newDescription
End Sub
#End RegionEnd Class
And I apply the attribute to various enumerations. For example:
Public Enum ConditionType
Intact
Fair
Poor
None
End EnumWhat I haven't be able to figure out exactly, is how to get one of the attribute's properties for a specific enumeration member. For example, if I have a variable
Dim condition As ConditionType = ConditionType.Fair
and I want to retrieve either theNewDescription
orOldDescription
property of the attribute tag ("Fair" or "F" respectively) how can I go about doing this. Currently I was using the following function to test out methods:Private Sub GetAttribute(T As Type)
Dim attr As OutputDescriptionAttribute = CType(Attribute.GetCustomAttribute(T, GetType(OutputDescriptionAttribute)), OutputDescriptionAttribute) Dim newDesc As String = attr.NewDescription Dim oldDesc As String = attr.OldDescription
End Sub
If I call this method using
GetAttribute(GetType(ConditionType))
I get no error message, however if I try doing the following:Dim condition As ConditionType = ConditionType.Fair
GetAttribute(condition.GetType)I get any error about ConditionType cannot be converted to a needed type. How can I go about getting the custom attribute that is applied to a specific enumeration member? Thanks in advance.
-
I designed a custom attribute using the following class:
_
Public Class OutputDescriptionAttribute
Inherits Attribute#Region "Variables"
Private _oldDescription As String
Private _newDescription As String
#End Region#Region "Properties"
Public ReadOnly Property OldDescription As String
Get
Return Me._oldDescription
End Get
End PropertyPublic ReadOnly Property NewDescription As String Get Return Me.\_newDescription End Get End Property
#End Region
#Region "Constructor"
Public Sub New(oldDescription As String, newDescription As String)
Me._oldDescription = oldDescription
Me._newDescription = newDescription
End Sub
#End RegionEnd Class
And I apply the attribute to various enumerations. For example:
Public Enum ConditionType
Intact
Fair
Poor
None
End EnumWhat I haven't be able to figure out exactly, is how to get one of the attribute's properties for a specific enumeration member. For example, if I have a variable
Dim condition As ConditionType = ConditionType.Fair
and I want to retrieve either theNewDescription
orOldDescription
property of the attribute tag ("Fair" or "F" respectively) how can I go about doing this. Currently I was using the following function to test out methods:Private Sub GetAttribute(T As Type)
Dim attr As OutputDescriptionAttribute = CType(Attribute.GetCustomAttribute(T, GetType(OutputDescriptionAttribute)), OutputDescriptionAttribute) Dim newDesc As String = attr.NewDescription Dim oldDesc As String = attr.OldDescription
End Sub
If I call this method using
GetAttribute(GetType(ConditionType))
I get no error message, however if I try doing the following:Dim condition As ConditionType = ConditionType.Fair
GetAttribute(condition.GetType)I get any error about ConditionType cannot be converted to a needed type. How can I go about getting the custom attribute that is applied to a specific enumeration member? Thanks in advance.
I've taken your class and put it in a console-application; below is the code that fetches the member, and then the attributes of said member.
Sub Main()
Dim someCondition As ConditionType = ConditionType.Fair
Dim mi As MemberInfo = GetType(ConditionType).GetMember(someCondition.ToString()).FirstOrDefault()
Dim attribute As OutputDescriptionAttribute = mi.GetCustomAttributes(GetType(OutputDescriptionAttribute)).FirstOrDefault()
Console.WriteLine(attribute.NewDescription)
Console.ReadLine()
End SubYou might want to put this into a nice little helper-method. Generics and method extensions would help in keeping above code readable.
Bastard Programmer from Hell :suss: If you can't read my code, try converting it here[^]