Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Find all parent types (both base classes and interfaces)

Tags:

c#

.net

I want to be able to find all parent types (base classes and interfaces) for a specific type.

EG if i have

class A : B, C { }
class B : D { }
interface C : E { }
class D { }
interface E { }

i want to see that A is B C D and E and Object

Whats the best way to do this? is there a reflection method to do this or do i need to make myself something.

====EDIT====

So something like this?

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;
            }
        }
    }

I was kinda hoping i didn't have to do it myself but oh well.

like image 641
Not loved Avatar asked Jan 15 '12 07:01

Not loved


People also ask

Is interface a parent class?

Interfaces are not classes, however, and an interface can extend more than one parent interface.

WHAT IS interface and base class?

An interface defines how other classes can use your code. A base class helps implementers to implement your interface.

When should I use an interface and when should I use a base class in C#?

My rule of thumb: If you want to provide some common functionality accessible to a subset of classes, use a base class. If you want to define the expected behavior of of a subset of classes, use an interface.

WHAT IS interfaces in C#?

An interface defines a contract. Any class or struct that implements that contract must provide an implementation of the members defined in the interface. Beginning with C# 8.0, an interface may define a default implementation for members.


1 Answers

More general solution:

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;
}

Or to actually get all parent types:

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;
    }
}
like image 111
Sergii Bogomolov Avatar answered Oct 25 '22 14:10

Sergii Bogomolov