Accessing EnumDisplayValue programmatically


Author
Message
Leonard P.
Leonard P.
StrataFrame Novice (93 reputation)StrataFrame Novice (93 reputation)StrataFrame Novice (93 reputation)StrataFrame Novice (93 reputation)StrataFrame Novice (93 reputation)StrataFrame Novice (93 reputation)StrataFrame Novice (93 reputation)StrataFrame Novice (93 reputation)StrataFrame Novice (93 reputation)
Group: Awaiting Activation
Posts: 65, Visits: 306
Hello,

Lets seay I have this enum type:

 public enum MyEnum
    {
        [EnumDisplayValue("This is Value 1")]
        value1 = 1,
        [EnumDisplayValue("This is Value 2")]
        value2 = 2
    }

Now consider this code:

   

MyEnum myEnum = MyEnum.value2;
MessageBox.Show(myEnum.ToString());

The messagebox displays "value2", but I would like to see EnumDisplayValue "This is Value 2".

Is there an easy way to get the value of that attribute? I have a feeling I need to use reflections to get it.

Thank you for your help.

Replies
Greg McGuffey
Greg McGuffey
Strategic Support Team Member (4.8K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Leonard,



This is what I use:



''' <summary>

''' Give an enum type and a value, return the display text for

''' that value. This will use the EnumDisplayValueAttribute if

''' the enum has one defined.

''' </summary>

''' <param name="enumType">type of enum</param>

''' <param name="value">value to lookup</param>

Public Shared Function GetEnumDisplayText(ByVal enumType As Type, ByVal value As Object) As String

  '-- Establish a return variable

  Dim desc As String = String.Empty



  Dim fi As FieldInfo = enumType.GetField(System.Enum.GetName(enumType, value))

  Dim descAttr As EnumDisplayValueAttribute = DirectCast(Attribute.GetCustomAttribute(fi, GetType(EnumDisplayValueAttribute)), EnumDisplayValueAttribute)

  If descAttr IsNot Nothing Then

    desc = descAttr.GetDisplayValue()

  Else

    desc = value.ToString()

  End If



  '-- Return the enum description/name

  Return desc

End Function


Leonard P.
Leonard P.
StrataFrame Novice (93 reputation)StrataFrame Novice (93 reputation)StrataFrame Novice (93 reputation)StrataFrame Novice (93 reputation)StrataFrame Novice (93 reputation)StrataFrame Novice (93 reputation)StrataFrame Novice (93 reputation)StrataFrame Novice (93 reputation)StrataFrame Novice (93 reputation)
Group: Awaiting Activation
Posts: 65, Visits: 306
Thanks Greg!

This is pretty much what I did at first (only with Generic method and looping through fields and attributes using reflections) :

private static string GetDisplayValueType<T>(Enum customEnum) where T : struct
        {

            string retValue = string.Empty;
           
            foreach (FieldInfo field in typeof(T).GetFields(BindingFlags.Static | BindingFlags.GetField | BindingFlags.Public))
            {

                object value = field.GetValue(null);


                foreach (Attribute attrib in field.GetCustomAttributes(true))
                {

                    if (attrib is EnumDisplayValueAttribute)
                    {

                        EnumDisplayValueAttribute customAttr = (EnumDisplayValueAttribute)attrib;
                        if (customEnum.Equals(value))
                        {
                            retValue = customAttr.ValueOrKey;
                            break;
                        }
                    }

                }

            }

            return retValue;

        }

Using your code, I was able to get rid of loops:

     private static string GetDisplayValueType1<T>(Enum customEnum) where T : struct
        {

            string desc = string.Empty;
            FieldInfo field = typeof(T).GetField(Enum.GetName(typeof(T), customEnum));
            EnumDisplayValueAttribute descrAttr = (EnumDisplayValueAttribute)Attribute.GetCustomAttribute(field, typeof(EnumDisplayValueAttribute));
            if (descrAttr != null)
              desc = descrAttr.ValueOrKey;           
            else
               desc = customEnum.ToString();

           return desc;

        }

The second function looks much cleaner Smile Thanks!

Greg McGuffey
Greg McGuffey
Strategic Support Team Member (4.8K reputation)
Group: Forum Members
Posts: 2K, Visits: 6.6K
Ooh, I like the generics....think I'll update my code to use it! Thanks back at ya! w00t
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...





Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search