我有一个继承自接口的类。该接口定义了一个我想在调用代码中订阅的事件。我尝试了几件事,但它们都解决了错误(我知道它是真的)。我如何检查一个类是否实现了特定的接口。检查通用类是否从接口继承
这里是我试过(注意,有问题的对象是实现MyInterface的一个用户控件,存储在控制器的阵列,其中只有一些实现MyInterface - 不是null):
if (this.controls[index].GetType().IsSubclassOf(typeof(MyInterface)))
((MyInterface)this.controls[index]).Event += this.Handler;
if (this.controls[index].GetType().IsAssignableFrom(typeof(MyInterface)))
((MyInterface)this.controls[index]).Event += this.Handler;
if (this.controls[index].GetType() == typeof(MyInterface))
((MyInterface)this.controls[index]).Event += this.Handler;
一切都无济于事。