Click here to Skip to main content
15,891,136 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Greeting All ,

I have this Enum type and I want to give a string value to criteria and value , how should I do ?

XML
public enum ItemsChoiceType1 {

    /// <remarks/>
    Criteria,

    /// <remarks/>
    Value,
}


I would like to convet ItemsChoiceType1 on string without adding any things to the enum type because it is provided by the API that I use
Please Help !
Posted
Updated 2-Apr-14 23:19pm
v2

1 solution

You can add Description attribute

C#
public enum ChartType : short
    {
        Pie = 1,
        Doughnut = 2,
        Column = 3,
        [Description("Column Stacked")]
        ColumnStacked = 4,
        Area = 5,
        [Description("Area Stacked")]
        AreaStacked = 6,
        [Description("Area Spline")]
        AreaSpline = 7,
        Bar = 8,
        [Description("Bar Stacked")]
        BarStacked = 9,
        Line = 10,
        Spline = 11
    }

You can get Description by below helper
public static class EnumExt
    {
        public static string Description(this Enum en)
        {
            string result = en.ToString();
            Type EnumType = en.GetType();

            FieldInfo fi = EnumType.GetField(result);
            var da = Attribute.GetCustomAttribute(fi, typeof(DescriptionAttribute)) as DescriptionAttribute;
            return (da != null ? da.Description : result);
        }
    }


C#
ChartType.BarStacked.Description()
 
Share this answer
 
v2
Comments
amiach 3-Apr-14 5:06am    
I cannot add any things to the type enum because it's provided by the API that I use .
I should add a string value in my client code and should not modify on the enum type .
In addition , I should change values of criteria and its value many times with the request that I will send .

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