Getting Enumeration Value using the [Description] Text

DPalkar
DPalkar used Ask the Experts™
on
Hi
I know the GetDescription Function (in attached code) will return me the Description value from the Enum MyEnum

My problem is I want to retrieve Enum Value based on the Description Text I pass in.
ie, if I pass "Myenum 1" the function should return me Enum1
if I pass "Myenum 2", it should return me Enum2.

I tried to do that using Function GetEnumValueFromDescription

but it throws an exception on
line  ->   foreach (Enum e in Enum.GetValues(MyEnum.GetType()))
saying it expects System.Type

please help,

Thanks
Deepak
public enum MyEnum 
{ 
[Description ("Myenum 1")] Enum1, 
[Description ("Myenum 2")] Enum2, 
[Description ("Myenum 3")] Enum3
}
 
        public static string GetDescription<T>(T EnumValue)
        {
            FieldInfo fi = EnumValue.GetType().GetField(EnumValue.ToString());
            DescriptionAttribute[] attributes = (DescriptionAttribute[])fi.GetCustomAttributes(typeof(DescriptionAttribute), false);
            return (attributes.Length > 0) ? attributes[0].Description : EnumValue.ToString();
        }
 
        public static Enum GetEnumValueFromDescription<T>(T MyEnum, string Description)
        {
            Enum retEnumValue = null;
 
            foreach (Enum e in Enum.GetValues(MyEnum.GetType()))
            {
                string sValue = GetDescription(MyEnum.GetType());
                if (sValue.ToLower() == Description.ToLower())
                {
                    retEnumValue = e;
                    break;
                }
            }
 
            return retEnumValue;
        }

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
try

foreach ( Enum e in Enum.GetValues( typeof(  MyEnum) ) )

Author

Commented:
Thanks for your reply

I used the typeof(MyEnum) before and I received the same error so I tried the GetType.
I think that's what I remember. Currently I am home, I'll try that out again tomorrow and get back to you.

thanks
Deepak

Author

Commented:
Hi Billy,

When I changed it to typeof(MyEnum), I get following compile time error.

The type or namespace name 'MyEnum' could not be found (are you missing a using directive or an assembly reference?)

thanks
Deepak
Exploring ASP.NET Core: Fundamentals

Learn to build web apps and services, IoT apps, and mobile backends by covering the fundamentals of ASP.NET Core and  exploring the core foundations for app libraries.

Commented:
I think this is because the compiler is confussed....
You have MyEnum as the public enum MyEnum and then in the constructor for the method you have (t MyEnum ...)

Can you add the namespace to this as well?
like...
typeof(DPalkar.MyEnum),

Does this class have a namespace?

Author

Commented:
Yes
it has a namespace but it's in the other library.
but if I hardcode it like DPalkar.MyEnum on that line, it won't be generalized, because I wanted to use the same function for any Enum.

Is there any other to do this may be through Reflection?
thanks
Deepak
Commented:
whay not 'add' the type to the overload so....

 public static Enum GetEnumValueFromDescription<T>( T MyEnum , Type Type_  , string Description )
        {
            Enum retEnumValue = null;
 
            foreach ( Enum e in Enum.GetValues( Type_ ) )

Open in new window

Author

Commented:
Great!!
In fact, I didn't even need the "T" either.
        public static Enum GetEnumValueFromDescription(Type MyType, string Description)
just the Type keyword did the trick.

thanks a lot and have a great Day!
Deepak
        public static Enum GetEnumValueFromDescription(Type MyType, string Description)
        {
            Enum retEnumValue = null;
 
            foreach (Enum e in Enum.GetValues(MyType))
            {
                string sValue = GetDescription((Enum)e);
                if (sValue.ToLower() == Description.ToLower())
                {
                    retEnumValue = e;
                    break;
                }
            }
 
            return retEnumValue;
        }

Open in new window

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial