加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 如何指定一个类而不是某个子类的对象?

发布时间:2020-12-15 17:46:11 所属栏目:百科 来源:网络整理
导读:class A{}class B : A{}void method(A that is not a B argument) {}void generic_method(generic_classA that is not a B generic_argument) {}void params_method(params A that is not a B[] params_arguments) {} 有没有任何语法的方式来做到这一点?我
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");

在第一种方法的开始,并且在第二和第三种方法中做到这一点,但是我想知道是否有一些关键字或者OOP概念来实现这一点.

解决方法

所以听起来你正在试图表达一个约束的相反,如:
var Foo<T>() where T : SomeClass

这将限制T成为SomeClass或子类1 …但是你试图使它明确地不是一个T.

不,我恐怕C#没有这样的限制.

1,假设SomeClass是一个开始的类,它可能是一个界面,具有你可能期望的意义.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读