<p>How can I get all implementations of an interface through reflection in C#?</p>

[{'answer_id': 80325, 'author': 'Alex Duggleby', 'author_id': 5790, 'author_profile': 'https://Stackoverflow.com/users/5790', 'pm_score': 1, 'selected': False, 'text': '<p>Do you mean all interfaces a Type implements?</p>\n\n<p>Like this:</p>\n\n<pre><code>ObjX foo = new ObjX();\nType tFoo = foo.GetType();\nType[] tFooInterfaces = tFoo.GetInterfaces();\nforeach(Type tInterface in tFooInterfaces)\n{\n  // do something with it\n}\n</code></pre>\n\n<p>Hope tha helpts.</p>\n'}, {'answer_id': 80343, 'author': 'Anton', 'author_id': 6464, 'author_profile': 'https://Stackoverflow.com/users/6464', 'pm_score': 3, 'selected': False, 'text': '<p>Have a look at <code>Assembly.GetTypes()</code> 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.</p>\n\n<p>On of the way to do so is using <code>Type.IsAssignableFrom</code> method.</p>\n\n<p>Here is the example. <code>myInterface</code> is the interface, implementations of which you are searching for.</p>\n\n<pre><code>Assembly myAssembly;\nType myInterface;\nforeach (Type type in myAssembly.GetTypes())\n{\n    if (myInterface.IsAssignableFrom(type))\n        Console.WriteLine(type.FullName);\n}\n</code></pre>\n\n<p>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.</p>\n'}, {'answer_id': 80375, 'author': 'Adam Driscoll', 'author_id': 13688, 'author_profile': 'https://Stackoverflow.com/users/13688', 'pm_score': 2, 'selected': False, 'text': '<pre><code>Assembly assembly = Assembly.GetExecutingAssembly();\nList&lt;Type&gt; types = assembly.GetTypes();\nList&lt;Type&gt; childTypes = new List&lt;Type&gt;();\nforeach (Type type in Types) {\n  foreach (Type interfaceType in type.GetInterfaces()) {\n       if (interfaceType.Equals(typeof([yourinterfacetype)) {\n            childTypes.Add(type)\n            break;\n       }\n  }\n}\n</code></pre>\n\n<p>Maybe something like that....</p>\n'}, {'answer_id': 80467, 'author': 'Steve Cooper', 'author_id': 6722, 'author_profile': 'https://Stackoverflow.com/users/6722', 'pm_score': 6, 'selected': False, 'text': '<p>The answer is this; it searches through the entire application domain -- that is, every assembly currently loaded by your application.</p>\n\n<pre><code>/// &lt;summary&gt;\n/// Returns all types in the current AppDomain implementing the interface or inheriting the type. \n/// &lt;/summary&gt;\npublic static IEnumerable&lt;Type&gt; TypesImplementingInterface(Type desiredType)\n{\n    return AppDomain\n           .CurrentDomain\n           .GetAssemblies()\n           .SelectMany(assembly =&gt; assembly.GetTypes())\n           .Where(type =&gt; desiredType.IsAssignableFrom(type));\n}\n</code></pre>\n\n<p>It is used like this;</p>\n\n<pre><code>var disposableTypes =  TypesImplementingInterface(typeof(IDisposable));\n</code></pre>\n\n<p>You may also want this function to find actual concrete types -- i.e., filtering out abstracts, interfaces, and generic type definitions.</p>\n\n<pre><code>public static bool IsRealClass(Type testType)\n{\n    return testType.IsAbstract == false\n         &amp;&amp; testType.IsGenericTypeDefinition == false\n         &amp;&amp; testType.IsInterface == false;\n}\n</code></pre>\n'}, {'answer_id': 81707, 'author': 'Hallgrim', 'author_id': 15454, 'author_profile': 'https://Stackoverflow.com/users/15454', 'pm_score': 1, 'selected': False, 'text': '<p>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.</p>\n'}, {'answer_id': 17267339, 'author': 'Sam', 'author_id': 238753, 'author_profile': 'https://Stackoverflow.com/users/238753', 'pm_score': 2, 'selected': False, 'text': '<p>Here are some <a href="http://msdn.microsoft.com/en-us/library/system.type.aspx" rel="nofollow noreferrer"><code>Type</code></a> extension methods that may be useful for this, as suggested by <a href="https://stackoverflow.com/users/35047/simon-farrow">Simon Farrow</a>. This code is just a restructuring of the accepted answer.</p>\n\n<h3>Code</h3>\n\n<pre><code>/// &lt;summary&gt;\n/// Returns all types in &lt;paramref name="assembliesToSearch"/&gt; that directly or indirectly implement or inherit from the given type. \n/// &lt;/summary&gt;\npublic static IEnumerable&lt;Type&gt; GetImplementors(this Type abstractType, params Assembly[] assembliesToSearch)\n{\n    var typesInAssemblies = assembliesToSearch.SelectMany(assembly =&gt; assembly.GetTypes());\n    return typesInAssemblies.Where(abstractType.IsAssignableFrom);\n}\n\n/// &lt;summary&gt;\n/// Returns the results of &lt;see cref="GetImplementors"/&gt; that match &lt;see cref="IsInstantiable"/&gt;.\n/// &lt;/summary&gt;\npublic static IEnumerable&lt;Type&gt; GetInstantiableImplementors(this Type abstractType, params Assembly[] assembliesToSearch)\n{\n    var implementors = abstractType.GetImplementors(assembliesToSearch);\n    return implementors.Where(IsInstantiable);\n}\n\n/// &lt;summary&gt;\n/// Determines whether &lt;paramref name="type"/&gt; is a concrete, non-open-generic type.\n/// &lt;/summary&gt;\npublic static bool IsInstantiable(this Type type)\n{\n    return !(type.IsAbstract || type.IsGenericTypeDefinition || type.IsInterface);\n}\n</code></pre>\n\n<h3>Examples</h3>\n\n<p>To get the instantiable implementors in the calling assembly:</p>\n\n<pre><code>var callingAssembly = Assembly.GetCallingAssembly();\nvar httpModules = typeof(IHttpModule).GetInstantiableImplementors(callingAssembly);\n</code></pre>\n\n<p>To get the implementors in the current AppDomain:</p>\n\n<pre><code>var appDomainAssemblies = AppDomain.CurrentDomain.GetAssemblies();\nvar httpModules = typeof(IHttpModule).GetImplementors(appDomainAssemblies);\n</code></pre>\n'}]
['https://Stackoverflow.com/questions/80247', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/-1/']