Click here to Skip to main content
15,886,753 members
Articles / Programming Languages / C#
Tip/Trick

Extending Enums with Custom Attributes

Rate me:
Please Sign up or sign in to vote.
4.38/5 (10 votes)
7 Mar 2011CPOL 47.9K   9   4
Extending Enums with Custom Attributes
Sometimes we are too lazy to create a class. Enumerations are very simple to create and use but they haven't any properties or fields. So, we can only add custom attributes (I used strings, but it's possible to use more complex types). I think the simplest way to do it is as follows:

C#
using System;
using System.Reflection;
namespace Test
{
    [AttributeUsage(AttributeTargets.Field)]
    public class ExtensionTest : Attribute
    {
        private string m_name;
        public ExtensionTest(string name)
        {
            this.m_name = name;
        }
        public static string Get(Type tp, string name)
        {
            MemberInfo[] mi = tp.GetMember(name);
            if (mi != null && mi.Length > 0)
            {
                ExtensionTest attr = Attribute.GetCustomAttribute(mi[0],
                    typeof(ExtensionTest)) as ExtensionTest;
                if (attr != null)
                {
                    return attr.m_name;
                }
            }
            return null;
        }
        public static string Get(object enm)
        {
            if (enm != null)
            {
                MemberInfo[] mi = enm.GetType().GetMember(enm.ToString());
                if (mi != null && mi.Length > 0)
                {
                    ExtensionTest attr = Attribute.GetCustomAttribute(mi[0],
                        typeof(ExtensionTest)) as ExtensionTest;
                    if (attr != null)
                    {
                        return attr.m_name;
                    }
                }
            }
            return null;
        }
    }
    public enum EnumTest
    {
        None,
        [ExtensionTest("(")] Left,
        [ExtensionTest(")")] Right,
    }
}

Usage:
C#
string txt = ExtensionTest.Get(typeof(EnumTest), "Left");
string txt = ExtensionTest.Get(EnumTest.Left)

License

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


Written By
Web Developer
Poland Poland
Complicated

Comments and Discussions

 
GeneralMy vote of 5 Pin
ChE Junkie11-Mar-20 8:41
ChE Junkie11-Mar-20 8:41 
GeneralReason for my vote of 1 No explanation of anything. Pin
FreeAsInBeer19-Jan-11 10:01
FreeAsInBeer19-Jan-11 10:01 
GeneralReason for my vote of 2 No explanation of anything. I could ... Pin
csb118-Jan-11 4:43
csb118-Jan-11 4:43 
Reason for my vote of 2
No explanation of anything. I could google for something like this. The purpose of an article is to share methods, etc with an explanation.
GeneralReason for my vote of 1 While the code looks workable, there... Pin
rbdavidson17-Jan-11 5:04
rbdavidson17-Jan-11 5:04 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.