我想要做的事情是这样的:我拥有具有组合标志值的枚举.
public static class EnumExtension
{
public static bool IsSet<T>( this T input, T matchTo )
where T:enum //the constraint I want that doesn't exist in C#3
{
return (input & matchTo) != 0;
}
}
所以我可以这样做:
MyEnum tester = MyEnum.FlagA | MyEnum.FlagB
if( tester.IsSet( MyEnum.FlagA ) )
//act on flag a
不幸的是,C#的通用WHERE约束没有枚举限制,只有class和struct.C#没有将枚举视为 struct (即使它们是值类型),所以我不能添加这样的扩展类型.
有人知道解决方法吗?