开发者

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

This question already has answers here: Closed 11 years ago.

Possible Duplicate:

Create Generic method constraining T to an Enum

Given a generic method that only ope开发者_如何学JAVArates 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.


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.


In C# you can't. For some reason I don't know the C# specification explicitly forbids System.Enum as a constraint.

And even then it wouldn't accept nullable enums because nullable enums are no enums.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜