class A { } class B : A { } void method(A that is not a B argument) {} void generic_method(generic_class<A that is not a B> generic_argument) {} void params_method(params A that is not a B[] params_arguments) {}
有没有任何语法的方式来做到这一点?我意识到我可以做
if(argument is B) throw new ArgumentException("argument cannot be a B","argument");
解决方法
所以听起来你正在试图表达一个约束的相反,如:
var Foo<T>() where T : SomeClass
这将限制T成为SomeClass或子类1 …但是你试图使它明确地不是一个T.
不,我恐怕C#没有这样的限制.
1,假设SomeClass是一个开始的类,它可能是一个界面,具有你可能期望的意义.