BlackWaspTM

This web site uses cookies. By using the site you accept the cookie policy.This message is for compliance with the UK ICO law.

Reflection
.NET 1.1+

Identifying all Types that Implement an Interface

Sometimes it is useful to find the list of classes in an assembly that implement a given interface. These implementers cannot be obtained directly from an interface but can be found indirectly using reflection.

Reflecting Interfaces

In the previous article I explained how you might find all of the classes in an assembly that are subclasses of a specific type. This can be useful when you are creating software that can use plug-ins, which may be unknown at the time of development. The main application can define a base class from which all plug-in types must be derived. In this situation, if you don't need any functionality in the base class you might wish to use an interface for the plug-ins instead. If you do, you'll need a different solution to identifying the potential plug-in classes; finding all types that implement your interface.

The Reflection tutorial describes some ways to use reflection to investigate implemented interfaces. We can use these features to obtain the list of types that implement our interface using steps similar to those in the previous article.

The steps are:

  1. Obtain a Type instance for the interface for which we wish to search.
  2. Get an array of the types in the target assembly.
  3. Loop through the types and use reflection methods to determine which of the types implement the target interface.

To demonstrate, create a new console application and add the following interface and classes. The interface is defined first, followed by seven classes in a three-level inheritance hierarchy. The final two classes are unrelated to the first seven and do not implement the interface. We will search the classes for those that implement IParent.

public interface IParent { }
public class Parent : IParent { }
public class Child1 : Parent { }
public class Child2 : Parent { }
public class Grandchild11 : Child1 { }
public class Grandchild12 : Child1 { }
public class Grandchild21 : Child2 { }
public class Grandchild22 : Child2 { }
public class NotAChild { }
public class NotAGrandchild : NotAChild { }

To identify the interface and find the types in the assembly we can use the typeof keyword against the interface and the GetTypes method for the target assembly. We'll work with the executing assembly. To add the code for the first two steps, add the following to the Main method.

Type parentType = typeof(IParent);
Assembly assembly = Assembly.GetExecutingAssembly();
Type[] types = assembly.GetTypes();

With the necessary type references obtained in an array, you can use GetInterfaces against each element, along with the Contains method, to determine which types implement IParent. The sample code below uses a LINQ query to achieve this. However the query could be substituted with nested foreach loops.

IEnumerable<Type> imp = types.Where(t => t.GetInterfaces().Contains(parentType));

foreach (Type type in imp)
{
    Console.WriteLine(type.Name);
}

/* OUTPUT

Parent
Child1
Child2
Grandchild11
Grandchild12
Grandchild21
Grandchild22

*/
13 April 2014