Several description attributes on emums

C # 4.0

If I want to set a description for the listing, I can use the DescriptionAttribute System.ComponentModel;

public enum EnumWithDescription
{
    [Description("EnumDescription1")]
    EE = 1,
    [Description("EnumDescription2")]
    PP
}

but if I need another specific description, I can implement my special attribute and extension method, which will return this additional description. eg:

public enum EnumWithDescription
{

    [MyDescritption("MyDescription1")]
    [Description("EnumDescription1")]
    EE = 1
}

anumValue.MyExtensionMethod(); // return me MyDescritpion stirng value

Maybe there are some more simple ways to do this?

+3
source share
2 answers

You can extend the DescriptionAttribute attribute as follows:

class ExtraDescriptionAttribute : DescriptionAttribute
    {
        private string extraInfo;
        public string ExtraInfo { get { return extraInfo; } set { extraInfo = value; } }

        public ExtraDescriptionAttribute(string description)
        {
            this.DescriptionValue = description;
            this.extraInfo = "";
        }
    }
+3
source

DescriptionAttribute, AllowMultiple false. - , , .

, , , , . , , , ( Unconstrained Melody), , , .

+1

Source: https://habr.com/ru/post/1758673/


All Articles