关于c#:System.Enum作为带约束的泛型类型参数

System.Enum as a generic type parameter with constraints

本问题已经有最佳答案,请猛点这里访问。

Possible Duplicate:
Create Generic method constraining T to an Enum
Enum type constraints in C#

考虑以下类别:

1
2
3
4
5
6
7
8
public class Transition<TState>
{
    public Transition ()
    {
        if (!typeof(TState).IsEnum)
            throw (new ArgumentException("[TState] has to be of type [System.Enum]."));
    }
}

理想情况下,这应该声明为:

1
2
3
public class Transition<TState> where TState: System.Enum
{
}

当然,上面的代码会产生编译时错误。我的问题是,为什么这是违法的。大多数消息来源解释说这是非法的,但没有解释为什么。有什么想法吗?


正如埃里克·利珀特所说,我引用

ALL features are unimplemented until someone designs, specs, implements, tests, documents and ships the feature. So far, no one has done that for this one. There's no particularly unusual reason why not; we have lots of other things to do, limited budgets, and this one has never made it past the"wouldn't this be nice?" discussion in the language design team."