locked
Get Enum From item (string) and Type - Runtime RRS feed

  • Question

  • User-450085702 posted

    Hi,

    I have an enum with not-so-friendly names, so wanted to use [Description("friendly name")] on few of the enum constants. For example, I have the below enum, when I show it to user they should be Created, Order Processing, Processed instead of Created, Processing, and Processed.

    public enum OrderStatus {

         Created,

         [Description("Order Processing")]  Processing,

         Processed

    }

    I have written this extension method that can be invoked on enum constant to get the friendly name from Description attribute.

    public static string GetDescription(this Enum enumValue)
    {
         var type = enumValue.GetType();
         var memberInfo = type.GetMember(enumValue.ToString());
         if (memberInfo.Length > 0)
         {
              var attrs = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute), false);
              if (attrs.Length > 0) return ((DescriptionAttribute)attrs[0]).Description;
         }
         return null;
    }

    This method works great if my call is made like this: OrderStatus.Processing.GetDescription(). But because I have tons of Enums that I render on form, I am using Reflection to make this call at runtime. Let's say I am rendering this as a dropdown on the form and the property looks like the following.

    [Field(Type = FieldType.Select, EnumType = typeof(OrderStatus))]

    public OrderStatus? OrderStatus  

    To provide the options in the Order Status dropdown, I am currently using Enum.GetNames(fieldAttribute.EnumType) but this doesn't consider the Description Attribute. 

    Question: How do I invoke .GetDescription extention method using EnumType and EnumName? I tried something like (fieldAttribute.EnumType)Enum.Parse(fieldAttribute.EnumType, EnumName).GetDescription() but type casting doesn't work this way.

    Thank you in advance for looking into this and trying to help.

    Regards,

    Siva

    Friday, May 22, 2020 3:51 PM

All replies

  • User-939850651 posted

    Hi, Hnqtxe1Hn@555

    According to your codes, I found  the GetDescription method cannot be called directly because it has not completed the conversion.

    I suggest you could try below codes and then it will work well.

    Method GetDescription :

    public static string GetDescription(this Enum enumValue)
            {
                var type = enumValue.GetType();
                var memberInfo = type.GetMember(enumValue.ToString());
                if (memberInfo.Length > 0)
                {
                    Object[] attrs = memberInfo[0].GetCustomAttributes(typeof(DescriptionAttribute),false);
                    if (attrs.Length > 0) {
                        return ((DescriptionAttribute)attrs[0]).Description;
                    }
                }
                return null;
            }
    

    OrderStatus.cs

    public enum OrderStatus
        {
            Created,
            [Description("Order Processing")]
            Processing,
            Processed
        }
    

    Test.cs :

    static void Main(string[] args)
            {
                String Des = "Processing";
                String v = ((OrderStatus)Enum.Parse(typeof(OrderStatus),Des)).GetDescription();
                Console.WriteLine("Description is : "+ v);
            }
    

    Result:

    result picture

    This should achieve what you want.

    Hope this can help you.

    Best regards,

    Xudong Peng

    Tuesday, May 26, 2020 8:53 AM
  • User-450085702 posted

    Hi Xudong,

    Thank you very much for your response. 

    Your code works perfectly fine, but in my case I don't know the Enum Type in compile time. So, I needed a generic way to convert the string to a type that will be decided in runtime.

    Luckily I was able to find the solution. I have just converted the enum constant to generic type Enum so that the extension method can be called on it. Here is how it looks.

    String v = (Enum.Parse(typeof(OrderStatus),Des) as Enum).GetDescription();

    Regards,

    Siva

    Saturday, June 6, 2020 4:47 PM