我需要某种方式来标记基本接口并标识类是否实现了基本接口或其派生接口。C#不允许使用“抽象接口”。有什么办法可以在C#中做到这一点?
public interface IBaseFoo
{
void BaseMethod();
}
public interface IFoo : IBaseFoo
{
void FooMethod();
}
public class Base
{
}
public class A : Base, IFoo
{
}
public class B : Base, IBaseFoo
{
}
现在,在下面的方法中,我需要检查是否typeCls
实现了IFoo
或IBaseFoo
没有明确指定类型。我需要一种标记基本接口并在方法中标识它的方法。(即:如果c#允许具有抽象接口,则可以检查IsAbstract
的接口的属性typeClas
)
public bool IsBaseFooImplemented<T>(T typeCls) where T : Base
{
// Here I need to check if the typeCls is implemented the IFoo or IBaseFoo
}
因为IFoo : IBaseFoo
,每个实现的类IFoo
也都实现IBaseFoo
。但不是相反的方式,因此您可以简单地检查是否typeCls is IFoo
。
请注意,基于已实现的接口更改行为通常是一种设计气味,首先会绕过接口的使用。
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句