Click here to Skip to main content
15,900,973 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm having an enum in which it has a description. i'm unable to get the description value. i managed to get the enum value but i want the value to be the description value.Any solutions?

What I have tried:

public enum FaqType
   {
       [Description("Internal")]
       Internal = 1,
       [Description("Website")]
       Website = 2,
       [Description("Web & Internal")]
       WebInternal = 3
   }



this is my entity in which requests and responses are handled.
[Description("Type")]
public string Type
{
    get
    {
        return ((Common.FaqType)TypeId).ToString();

    }
}
Posted
Updated 11-Nov-18 4:19am

1 solution

C#
public static string ToDescription(this System.Enum data)
        {
            var field = data.GetType().GetField(data.ToString());
            var desAttribute = field.GetCustomAttributes(typeof(DescriptionAttribute), false) as DescriptionAttribute[];

            if (desAttribute.Length > 0)
            {
                return desAttribute[0].Message;
            }
            return string.Empty;
        }



Where Description is Custom Attribute having Message property which you set in Constructor
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900