C# c中的基本接口#

C# c中的基本接口#,c#,reflection,interface,C#,Reflection,Interface,我需要某种方法来标记基接口,并确定类是实现了基接口还是其派生接口。c#不允许有“抽象接口”。在c#中有什么方法可以做到这一点吗 现在在下面的方法中,我需要检查typeCls是否在没有明确指定类型的情况下由IFoo或IBaseFoo实现。我需要一种方法来标记基本接口并在方法中识别它。(即:如果c#允许有抽象接口,我可以检查是否为typeClas接口的属性) public bool isbasefoooimplemented(T typeCls),其中T:Base { //这里我需要检查typeCl

我需要某种方法来标记基接口,并确定类是实现了基接口还是其派生接口。c#不允许有“抽象接口”。在c#中有什么方法可以做到这一点吗

现在在下面的方法中,我需要检查
typeCls
是否在没有明确指定类型的情况下由
IFoo
IBaseFoo
实现。我需要一种方法来标记基本接口并在方法中识别它。(即:如果c#允许有抽象接口,我可以检查
是否为
typeClas
接口的属性)

public bool isbasefoooimplemented(T typeCls),其中T:Base
{
//这里我需要检查typeCls是由IFoo还是IBaseFoo实现的
}

因为
IFoo:IBaseFoo
,实现
IFoo
的每个类也实现了
IBaseFoo
。但不是相反,因此您可以简单地检查
typeCls是否为IFoo

请注意,基于实现的接口更改行为通常是一种设计气味,它首先绕过了接口的使用。

//define
//somewhere define 

static List<IBaseFoo> list = new List<IBaseFoo>();

public class A : Base, IFoo
{
    public A()
    {
        YourClass.list.add(this);
    }
}

public class B : Base, IBaseFoo
{
    public B()
    {
        YourClass.list.add(this);
    }
}
静态列表=新列表(); 公共A类:基本类,IFoo { 公共A() { YourClass.list.add(这个); } } 公共B类:基本,IBaseFoo { 公共图书馆B() { YourClass.list.add(这个); } }
//然后可以检查类是否为IFoo

public bool IsBaseFooImplemented<T>(T typeCls) where T : Base
{
     foreach(var c in list )
     {
         if(typeof(c) == typeCls) return true;
     }
     return false;
}
public bool isbasefoooimplemented(T typeCls),其中T:Base
{
foreach(列表中的变量c)
{
if(typeof(c)=typeCls)返回true;
}
返回false;
}

我还没有测试代码,但它应该可以工作。

这有点异味。你说你“需要”检查这个-为什么你需要检查?您认为标准的SOLID OO无法提供您想要实现的目标是什么?因为你将要打开/关闭,所以在打开/关闭之前要仔细考虑。
//somewhere define 

static List<IBaseFoo> list = new List<IBaseFoo>();

public class A : Base, IFoo
{
    public A()
    {
        YourClass.list.add(this);
    }
}

public class B : Base, IBaseFoo
{
    public B()
    {
        YourClass.list.add(this);
    }
}
public bool IsBaseFooImplemented<T>(T typeCls) where T : Base
{
     foreach(var c in list )
     {
         if(typeof(c) == typeCls) return true;
     }
     return false;
}