Click here to Skip to main content
15,892,746 members
Articles / Programming Languages / C#

Generic Type Restriction for Enums

Rate me:
Please Sign up or sign in to vote.
4.43/5 (3 votes)
10 Jul 2011LGPL3 16.7K   6   6
How to restrict a generic type identifier to only allow enums

I was looking for a way to restrict a generic type identifier to only allow enums. Since you are not allowed to use where T : Enum, I checked the metadata for Enum and found that it implements ValueType, IComparable, IFormattable, IConvertible. You can't use ValueType as restriction, but struct works fine.

In other words, use “where T : struct, IComparable, IFormattable, IConvertible” to make a type restriction which should work well as an enum restriction.

The reason why I needed the specification is that I want to be able to fetch the DescriptionAttribute from enums. I use it to be able to add friendly names to enum values.

Example enum:

C#
public enum MyEnum
{
    [Description("Some value"]
    SomeValue,

    [Description("Foo bar")]
    FooBar
}

Fetch value:

C#
MyEnum value = MyEnum.FooBar;

// will display "Foo bar"
Console.WriteLine(value.GetDescription());

The extension method that does the magic:

C#
public static string GetDescription<T>(this T instance)
    where T : struct, IConvertible, IFormattable, IComparable
{
    var type = typeof (T);
    var memberInfo = type.GetMember(instance.ToString())[0];

    var attribute = (DescriptionAttribute)memberInfo.GetCustomAttributes(
        typeof(DescriptionAttribute), false).FirstOrDefault();
    return attribute == null ? instance.ToString() : attribute.Description;
}

License

This article, along with any associated source code and files, is licensed under The GNU Lesser General Public License (LGPLv3)


Written By
Founder 1TCompany AB
Sweden Sweden

Comments and Discussions

 
GeneralMy vote of 3 Pin
Richard Deeming13-Jul-11 8:20
mveRichard Deeming13-Jul-11 8:20 
Suggestionnicely done Pin
Ian Good12-Jul-11 8:02
Ian Good12-Jul-11 8:02 
GeneralRe: nicely done Pin
jgauffin12-Jul-11 19:34
jgauffin12-Jul-11 19:34 
GeneralRe: nicely done Pin
Ian Good13-Jul-11 16:33
Ian Good13-Jul-11 16:33 
I'm not sure I understand why you would need to use generics in this case.

Using Enum as the parameter type is the same thing as where T : Enum (if that worked) because Enum is the base class of all enum types.

This would be a different story if we needed to do something more interesting like return a value of the same type.
But we don't care what the actual type is, only that it derives from Enum.
Ian Good
 Developer at 5TH Cell
  Wallpaperr - automatic wallpaper changer

GeneralRe: nicely done Pin
jgauffin13-Jul-11 19:43
jgauffin13-Jul-11 19:43 
GeneralRe: nicely done Pin
Ian Good14-Jul-11 6:49
Ian Good14-Jul-11 6:49 

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.