查找所有父类型(基类和接口)

Not*_*ple 14 .net c#

我希望能够找到特定类型的所有父类型(基类和接口).

EG如果我有

class A : B, C { }
class B : D { }
interface C : E { }
class D { }
interface E { }
Run Code Online (Sandbox Code Playgroud)

我想看到A BCD和E和对象

什么是最好的方法呢?有没有一种反射方法来做到这一点,还是我需要做一些事情.

==== ====编辑

这样的事情呢?

public static IEnumerable<Type> ParentTypes(this Type type)
    {
        foreach (Type i in type.GetInterfaces())
        {
            yield return i;
            foreach (Type t in i.ParentTypes())
            {
                yield return t;
            }
        }

        if (type.BaseType != null)
        {
            yield return type.BaseType;
            foreach (Type b in type.BaseType.ParentTypes())
            {
                yield return b;
            }
        }
    }
Run Code Online (Sandbox Code Playgroud)

我有点希望自己不必自己做,但是哦.

Ser*_*lov 29

更一般的解决方案

public static bool InheritsFrom(this Type type, Type baseType)
{
    // null does not have base type
    if (type == null)
    {
        return false;
    }

    // only interface or object can have null base type
    if (baseType == null)
    {
        return type.IsInterface || type == typeof(object);
    }

    // check implemented interfaces
    if (baseType.IsInterface)
    {
        return type.GetInterfaces().Contains(baseType);
    }

    // check all base types
    var currentType = type;
    while (currentType != null)
    {
        if (currentType.BaseType == baseType)
        {
            return true;
        }

        currentType = currentType.BaseType;
    }

    return false;
}
Run Code Online (Sandbox Code Playgroud)

或者实际获取所有父类型:

public static IEnumerable<Type> GetParentTypes(this Type type)
{
    // is there any base type?
    if (type == null)
    {
        yield break;
    }

    // return all implemented or inherited interfaces
    foreach (var i in type.GetInterfaces())
    {
        yield return i;
    }

    // return all inherited types
    var currentBaseType = type.BaseType;
    while (currentBaseType != null)
    {
        yield return currentBaseType;
        currentBaseType= currentBaseType.BaseType;
    }
}
Run Code Online (Sandbox Code Playgroud)

  • `GetParentTypes` 是有道理的。但为什么你想要“InheritsFrom”?内置的 IsAssignableFrom 还不够吗? (3认同)

Ani*_*Ani 7

要获取由类型实现的接口,请使用Type.GetInterfaces.要查看其类层次结构,您可以Type.BaseType迭代使用,直到您点击一个null参考(通常这会在您点击之后发生System.Object,但不一定 - 例如,接口类型的基本类型将直接发生null).


Luk*_*uke 6

AC#扩展方法为懒:

/// <summary>
/// Extension method to check the entire inheritance hierarchy of a
/// type to see whether the given base type is inherited.
/// </summary>
/// <param name="t">The Type object this method was called on</param>
/// <param name="baseType">The base type to look for in the 
/// inheritance hierarchy</param>
/// <returns>True if baseType is found somewhere in the inheritance 
/// hierarchy, false if not</returns>
public static bool InheritsFrom(this Type t, Type baseType)
{
    Type cur = t.BaseType;

    while (cur != null)
    {
        if (cur.Equals(baseType))
        {
            return true;
        }

        cur = cur.BaseType;
    }

    return false;
}
Run Code Online (Sandbox Code Playgroud)