那你不妨将这个方法直接定义成一个委托,由子类实现或对这个委托进行配置,实现你的效果。你的委托可以类似这样写public abstract class A<T1> { public Func<T1, T1, T1> GetMixType; }public class B : A <B.MyType1> { public B() { GetMixType = _GetMixType; } private MyType1 _GetMixType(MyType1 x, MyType1 y) { return x ^ y; } public enum MyType1 { a, b, c } }
调用也贴下把。应该很直观了 B x = new B(); x.GetMixType(B.MyType1.a, B.MyType1.b);
[FlagsAttribute]
public enum FlagsTest : int
{
a1=0,
a2=1,
a3=2,
a4=4,
a5=8,
a6=16 }关键是每个枚举成员必须是2的N(0,1,2....)次方,不能有重复的
{
if (v is Enum == false ||
Enum.GetUnderlyingType( v.GetType() ) != typeof(int) ||
System.ComponentModel.TypeDescriptor.GetAttributes(v)[typeof(FlagsAttribute)] == null)
{
throw new Exception("v is not a flagged enum"); //<----
}
}
或者
static void Test(Enum v)
{
//...
}
{
a1=0,
a2=1,
a3=2,
a4=4,
a5=8,
a6=16
}
根据需求,不一定要是2的次方,最好不要硬性规定每个玫举的值,(书上说的)
static void Test(Enum v)
{
//...
}但是不能保证这类型 [FlagsAttribute] 和 int的
{
public Func<T1, T1, T1> GetMixType;
}public class B : A <B.MyType1>
{
public B()
{
GetMixType = _GetMixType;
} private MyType1 _GetMixType(MyType1 x, MyType1 y)
{
return x ^ y;
} public enum MyType1 { a, b, c }
}
B x = new B();
x.GetMixType(B.MyType1.a, B.MyType1.b);