Click here to Skip to main content
11,478,562 members (65,320 online)
Click here to Skip to main content

Tagged as

Extending Enums with Custom Attributes

, 7 Mar 2011 CPOL 12.4K 7
Rate this:
Please Sign up or sign in to vote.
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:

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:
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)

Share

About the Author

A.J.Wegierski
Web Developer
Poland Poland
Complicated

Comments and Discussions

 
GeneralReason for my vote of 1 No explanation of anything. Pin
Vivi_BlckMage19-Jan-11 11:01
memberVivi_BlckMage19-Jan-11 11:01 
GeneralReason for my vote of 2 No explanation of anything. I could ... Pin
csb118-Jan-11 5:43
membercsb118-Jan-11 5:43 
GeneralReason for my vote of 1 While the code looks workable, there... Pin
rbdavidson17-Jan-11 6:04
memberrbdavidson17-Jan-11 6:04 

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

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

| Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.150520.1 | Last Updated 8 Mar 2011
Article Copyright 2011 by A.J.Wegierski
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid