Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. Visual Basic
  4. Retrieving Custom Attributes on Enumeration Members

Retrieving Custom Attributes on Enumeration Members

Scheduled Pinned Locked Moved Visual Basic
tutorialquestionhelp
2 Posts 2 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • D Offline
    D Offline
    Dominick Marciano
    wrote on last edited by
    #1

    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 Property

    Public 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 Region

    End Class

    And I apply the attribute to various enumerations. For example:

    Public Enum ConditionType
    Intact
    Fair
    Poor
    None
    End Enum

    What 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 the NewDescription or OldDescription 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.

    L 1 Reply Last reply
    0
    • D Dominick Marciano

      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 Property

      Public 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 Region

      End Class

      And I apply the attribute to various enumerations. For example:

      Public Enum ConditionType
      Intact
      Fair
      Poor
      None
      End Enum

      What 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 the NewDescription or OldDescription 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.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      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 Sub

      You 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[^]

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups