Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Generic C# method taking where the enum value as a parameter [duplicate]

Possible Duplicate:
Create Generic method constraining T to an Enum

Given a generic method that only operates on enum values

static void <T> method(T enum) where T ?????
{
     // do something with enum...
}

How do I constrain T such that only enum values are accepted? I've tried using struct however this disallows the use calling my method with a nullable enum type.

like image 820
Carlo V. Dango Avatar asked Sep 08 '11 11:09

Carlo V. Dango


1 Answers

Generic constraints on enum types are impossible in C#, but are possible in IL. Have a look at Jon Skeet's project, Unconstrained Melody, it will allow you to constraint your generic methods to enums.

like image 130
Igal Tabachnik Avatar answered Sep 27 '22 17:09

Igal Tabachnik