System.Enum作为具有约束的泛型类型参数

时间:2012-09-24 05:40:08

标签: c# .net generics enums constraints

  

可能重复:
  Create Generic method constraining T to an Enum
  Enum type constraints in C#

考虑以下课程:

public class Transition<TState>
{
    public Transition ()
    {
        if (!typeof(TState).IsEnum)
            throw (new ArgumentException("[TState] has to be of type [System.Enum]."));
    }
}

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

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

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

1 个答案:

答案 0 :(得分:3)

作为Eric Lippert says和我引用

  

所有功能都未实现,直到某人设计,规格,工具,测试,文档和发布该功能。到目前为止,没有人为此做过。没有特别不寻常的原因;我们还有许多其他的事情要做,预算有限,而且这个从来没有超越过“这不是很好吗?”语言设计团队的讨论。“

相关问题