Search code examples
c#.netsystem.reflection

Get all types and interfaces a class inherits from and implements in C#


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?


Solution

  • You could get all in one go using something like:

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

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