小编Pan*_*321的帖子

检查类型是否直接实现接口。并非来自继承

从类型 A、B、C、ANotState、BNotState 列表中,我想获取标有接口的 A、B、C。但是,如果确实从 ANotState 或 BNotState 获取接口,我只能得到从 C 继承的 isState(在 BNotState 情况下从 ANotState 继承)。

有没有办法知道接口是否是直接实现的?

public class Program
{
    public static void Main()
    {
        List<Type> types = new List<Type>{typeof(A), typeof(B), typeof(C), typeof(ANotState), typeof(BNotState)};
        
        //Now remove from list types that are not implementing isState interface directly which means ANotState and BNotState
    }
}

public interface isState{}


public class A : isState
{
    
}

public abstract class B : A, isState
{

}

public abstract class C : B, isState
{ …
Run Code Online (Sandbox Code Playgroud)

c# inheritance interface

1
推荐指数
1
解决办法
959
查看次数

标签 统计

c# ×1

inheritance ×1

interface ×1