1

I saw this question which is similar to mine:

How to find all the types in an Assembly that Inherit from a Specific Type C#

However, what if my class implements multiple interfaces as well:

class MyClass: MyBaseClass, IMyInterface1, IMyInterface2

Can I somehow get an array of all the stuff MyClass implements, and not just going one by one?

Community
  • 1
  • 1
Mefhisto1
  • 2,188
  • 7
  • 34
  • 73

5 Answers5

4

For interfaces you can call Type.GetInterfaces()

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
4

If you are interested in all the base types plus interfaces you can use:

static Type[] BaseTypesAndInterfaces(Type type) 
{
    var lst = new List<Type>(type.GetInterfaces());

    while (type.BaseType != null) 
    {
        lst.Add(type.BaseType);
        type = type.BaseType;
    }

    return lst.ToArray();
}

Use it like:

var x = BaseTypesAndInterfaces(typeof(List<MyClass>));

It's even possible to make it generic-based

static Type[] BaseTypesAndInterfaces<T>() 
{
    Type type = typeof(T);

    var lst = new List<Type>(type.GetInterfaces());

    while (type.BaseType != null) 
    {
        lst.Add(type.BaseType);
        type = type.BaseType;
    }

    return lst.ToArray();
}

and

var x = BaseTypesAndInterfaces<MyClass>();

but it's probably less interesting (because normally you "discover" MyClass at runtime, so you can't easily use generic methods with it)

xanatos
  • 109,618
  • 12
  • 197
  • 280
  • 1
    Is that `1st` or `lst`, I can't tell... well of course I can after my brain uses a single cell more than it would have had to if you'd have just written `list`. I don't get it, man. Why no human-readable oriented variable/thing naming!? The premise is just lazy. And the contradictory and hypocritical part is, to use the method you have to type out a comparatively HUGE string of `BaseTypesAndInterfaces`. – Grant Thomas Feb 19 '15 at 13:34
  • 1
    @GrantThomas It's `lst` with `l` of `list`, because in C# variable names can't start with numbers :-) And on the similarity of `l` with `I` and `1` you should talk to the ones that create fonts :-) – xanatos Feb 19 '15 at 13:35
  • I appreciate everything you've just said, and take that into account, but it doesn't justify it. I'm attacking the premise, the principle of this cult of keeping things short, arbitrarily. It just doesn't make sense to a thinking human. As for your only valid point, about variables starting with numbers, that's actually a good reason to be explicit when on a forum potentially (and mostly) interacting with novices. – Grant Thomas Feb 19 '15 at 13:37
3

If you want to combine interfaces with the base type into a single array, you can do this:

var t = typeof(MyClass);
var allYourBase = new[] {t.BaseType}.Concat(t.GetInterfaces()).ToArray();

Note that your array would contain all bases, including System.Object. This will not work for System.Object, because its base type is null.

Sergey Kalinichenko
  • 714,442
  • 84
  • 1,110
  • 1,523
1

You could get all in one go using something like:

var allInheritance = type.GetInterfaces().Union(new[] { type.BaseType});

Live example: http://rextester.com/QQVFN51007

Jamiec
  • 133,658
  • 13
  • 134
  • 193
0

Here's the extension method I use:

public static IEnumerable<Type> EnumInheritance(this Type type)
{
    while (type.BaseType != null)
        yield return type = type.BaseType;
    foreach (var i in type.GetInterfaces())
        yield return i;
}
dahall
  • 318
  • 3
  • 6