<分区>
Possible Duplicate:
Create Generic method constraining T to an Enum
给定一个仅对枚举值进行操作的通用方法
static void <T> method(T enum) where T ?????
{
// do something with enum...
}
如何约束 T
以便只接受枚举值?我试过使用 struct
但是这不允许使用可为 null 的枚举类型调用我的方法。
<分区>
Possible Duplicate:
Create Generic method constraining T to an Enum
给定一个仅对枚举值进行操作的通用方法
static void <T> method(T enum) where T ?????
{
// do something with enum...
}
如何约束 T
以便只接受枚举值?我试过使用 struct
但是这不允许使用可为 null 的枚举类型调用我的方法。
最佳答案
枚举类型的通用约束在 C# 中是不可能的,但在 IL 中是可能的。查看 Jon Skeet 的项目,Unconstrained Melody ,它将允许您将泛型方法限制为枚举。
关于c# - 将枚举值作为参数的通用 C# 方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7347011/