【发布时间】:2016-09-05 11:06:45
【问题描述】:
我需要某种方式来标记基接口并确定一个类是实现了基接口还是它的派生接口。 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#允许有抽象接口,我可以检查IsAbstracttypeClas的接口属性)
public bool IsBaseFooImplemented<T>(T typeCls) where T : Base
{
// Here I need to check if the typeCls is implemented the IFoo or IBaseFoo
}
【问题讨论】:
-
这有点味道。你说你“需要”检查这个 - 为什么你需要检查?您认为标准 SOLID OO 无法实现的目标是什么?因为你即将打开/关闭,所以在你做之前要仔细考虑。
标签: c# reflection interface