40

How can I get all implementations of an interface through reflection in C#?

Sam
  • 40,644
  • 36
  • 176
  • 219

6 Answers6

59

The answer is this; it searches through the entire application domain -- that is, every assembly currently loaded by your application.

/// <summary>
/// Returns all types in the current AppDomain implementing the interface or inheriting the type. 
/// </summary>
public static IEnumerable<Type> TypesImplementingInterface(Type desiredType)
{
    return AppDomain
           .CurrentDomain
           .GetAssemblies()
           .SelectMany(assembly => assembly.GetTypes())
           .Where(type => desiredType.IsAssignableFrom(type));
}

It is used like this;

var disposableTypes =  TypesImplementingInterface(typeof(IDisposable));

You may also want this function to find actual concrete types -- i.e., filtering out abstracts, interfaces, and generic type definitions.

public static bool IsRealClass(Type testType)
{
    return testType.IsAbstract == false
         && testType.IsGenericTypeDefinition == false
         && testType.IsInterface == false;
}
Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
Steve Cooper
  • 20,542
  • 15
  • 71
  • 88
  • 8
    Note that this code can't see the future -- it can't find implementation of this interface in interfaces that have not yet been loaded. – Aaron Sep 18 '08 at 09:49
  • 1
    I think that I might turn this into an extension method for Type, might turn out to be very useful. – Simon Farrow Feb 16 '09 at 11:28
  • Steve: Can you shed any light on a similar problem I'm having: http://stackoverflow.com/questions/5849210/net-getting-all-implementations-of-a-generic-interface – David Pfeffer May 01 '11 at 15:12
  • 1
    You can simplify `IsRealClass` to `return !(testType.IsAbstract || testType.IsGenericTypeDefinition || testType.IsInterface);` – Sam Jun 24 '13 at 01:48
  • .SelectMany(assembly => assembly.GetTypes()) this line gave me following exception: {"Could not load file or assembly 'Microsoft.WindowsAzure.MobileServices.Management, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f300afd708cefcd3' or one of its dependencies. The system cannot find the file specified.":"Microsoft.WindowsAzure.MobileServices.Management, Version=1.0.0.0, Culture=neutral, PublicKeyToken=f300afd708cefcd3"} – hakan Apr 14 '15 at 10:39
  • @piedpiper - my guess is that (a) your project references that file, (b) In the project properties, it's been marked 'copy local=false', and maybe (c) no code references anything in it. So your code compiles, but when reflection comes to load the type, it can't find the assembly in question and craps out. I'd suggest removing the reference if it's not used. Hope that's some help... – Steve Cooper Apr 16 '15 at 13:33
5

Have a look at Assembly.GetTypes() method. It returns all the types that can be found in an assembly. All you have to do is to iterate through every returned type and check if it implements necessary interface.

On of the way to do so is using Type.IsAssignableFrom method.

Here is the example. myInterface is the interface, implementations of which you are searching for.

Assembly myAssembly;
Type myInterface;
foreach (Type type in myAssembly.GetTypes())
{
    if (myInterface.IsAssignableFrom(type))
        Console.WriteLine(type.FullName);
}

I do believe that it is not a very efficient way to solve your problem, but at least, it is a good place to start.

Patrick D'Souza
  • 3,491
  • 2
  • 22
  • 39
Anton
  • 978
  • 5
  • 11
4
Assembly assembly = Assembly.GetExecutingAssembly();
List<Type> types = assembly.GetTypes();
List<Type> childTypes = new List<Type>();
foreach (Type type in Types) {
  foreach (Type interfaceType in type.GetInterfaces()) {
       if (interfaceType.Equals(typeof([yourinterfacetype)) {
            childTypes.Add(type)
            break;
       }
  }
}

Maybe something like that....

Adam Driscoll
  • 9,395
  • 9
  • 61
  • 104
3

Here are some Type extension methods that may be useful for this, as suggested by Simon Farrow. This code is just a restructuring of the accepted answer.

Code

/// <summary>
/// Returns all types in <paramref name="assembliesToSearch"/> that directly or indirectly implement or inherit from the given type. 
/// </summary>
public static IEnumerable<Type> GetImplementors(this Type abstractType, params Assembly[] assembliesToSearch)
{
    var typesInAssemblies = assembliesToSearch.SelectMany(assembly => assembly.GetTypes());
    return typesInAssemblies.Where(abstractType.IsAssignableFrom);
}

/// <summary>
/// Returns the results of <see cref="GetImplementors"/> that match <see cref="IsInstantiable"/>.
/// </summary>
public static IEnumerable<Type> GetInstantiableImplementors(this Type abstractType, params Assembly[] assembliesToSearch)
{
    var implementors = abstractType.GetImplementors(assembliesToSearch);
    return implementors.Where(IsInstantiable);
}

/// <summary>
/// Determines whether <paramref name="type"/> is a concrete, non-open-generic type.
/// </summary>
public static bool IsInstantiable(this Type type)
{
    return !(type.IsAbstract || type.IsGenericTypeDefinition || type.IsInterface);
}

Examples

To get the instantiable implementors in the calling assembly:

var callingAssembly = Assembly.GetCallingAssembly();
var httpModules = typeof(IHttpModule).GetInstantiableImplementors(callingAssembly);

To get the implementors in the current AppDomain:

var appDomainAssemblies = AppDomain.CurrentDomain.GetAssemblies();
var httpModules = typeof(IHttpModule).GetImplementors(appDomainAssemblies);
Community
  • 1
  • 1
Sam
  • 40,644
  • 36
  • 176
  • 219
1

Do you mean all interfaces a Type implements?

Like this:

ObjX foo = new ObjX();
Type tFoo = foo.GetType();
Type[] tFooInterfaces = tFoo.GetInterfaces();
foreach(Type tInterface in tFooInterfaces)
{
  // do something with it
}

Hope tha helpts.

Alex Duggleby
  • 7,948
  • 5
  • 37
  • 44
1

You have to loop over all assemblies that you are interested in. From the assembly you can get all the types it defines. Note that when you do AppDomain.CurrentDomain.Assemblies you only get the assemblies that are loaded. Assemblies are not loaded until they are needed, so that means that you have to explicitly load the assemblies before you start searching.

Hallgrim
  • 15,143
  • 10
  • 46
  • 54