Click here to Skip to main content
Licence LGPL3
First Posted 10 Jul 2011
Views 4,424
Bookmarked 5 times

Generic type restriction for enums

By | 10 Jul 2011 | Technical Blog
How to restrict a generic type identifier to only allow enums.
A Technical Blog article. View original blog here.[^]

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:

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

    [Description("Foo bar")]
    FooBar
}

Fetch value:

MyEnum value = MyEnum.FooBar;

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

The extension method that does the magic:

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)

About the Author

jgauffin

Founder
Gauffin Interactive AB
Sweden Sweden

Member

Follow on Twitter Follow on Twitter
Freelance developer/architect with a passion for code quality, architecture, refactoring, networking and threading.
 
Solid skills in .NET/C#/MVC3
 
Blog: http://blog.gauffin.org

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralMy vote of 3 PinmemberRichard Deeming8:20 13 Jul '11  
Suggestionnicely done PinmemberIGood8:02 12 Jul '11  
GeneralRe: nicely done Pinmemberjgauffin19:34 12 Jul '11  
GeneralRe: nicely done PinmemberIGood16:33 13 Jul '11  
GeneralRe: nicely done Pinmemberjgauffin19:43 13 Jul '11  
GeneralRe: nicely done PinmemberIGood6:49 14 Jul '11  

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.

Permalink | Advertise | Privacy | Mobile
Web01 | 2.5.120517.1 | Last Updated 10 Jul 2011
Article Copyright 2011 by jgauffin
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid