<p>Using reflection, how can I get all types that implement an interface with C# 3.0/.NET 3.5 with the least code, and minimizing iterations?</p>

<p>This is what I want to re-write:</p>

<pre><code>foreach (Type t in this.GetType().Assembly.GetTypes())
    if (t is IMyInterface)
        ; //do stuff
</code></pre>

[{'answer_id': 26745, 'author': 'Lasse V. Karlsen', 'author_id': 267, 'author_profile': 'https://Stackoverflow.com/users/267', 'pm_score': 4, 'selected': False, 'text': "<p>loop through all loaded assemblies, loop through all their types, and check if they implement the interface.</p>\n\n<p>something like:</p>\n\n<pre><code>Type ti = typeof(IYourInterface);\nforeach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) {\n    foreach (Type t in asm.GetTypes()) {\n        if (ti.IsAssignableFrom(t)) {\n            // here's your type in t\n        }\n    }\n}\n</code></pre>\n"}, {'answer_id': 26750, 'author': 'Darren Kopp', 'author_id': 77, 'author_profile': 'https://Stackoverflow.com/users/77', 'pm_score': 11, 'selected': True, 'text': '<p>Mine would be this in c# 3.0 :)</p>\n\n<pre><code>var type = typeof(IMyInterface);\nvar types = AppDomain.CurrentDomain.GetAssemblies()\n    .SelectMany(s =&gt; s.GetTypes())\n    .Where(p =&gt; type.IsAssignableFrom(p));\n</code></pre>\n\n<p>Basically, the least amount of iterations will always be:</p>\n\n<pre><code>loop assemblies  \n loop types  \n  see if implemented.\n</code></pre>\n'}, {'answer_id': 26752, 'author': 'tags2k', 'author_id': 192, 'author_profile': 'https://Stackoverflow.com/users/192', 'pm_score': 2, 'selected': False, 'text': '<p>Edit: I\'ve just seen the edit to clarify that the original question was for the reduction of iterations / code and that\'s all well and good as an exercise, but in real-world situations you\'re going to want the fastest implementation, regardless of how cool the underlying LINQ looks.</p>\n\n<p>Here\'s my Utils method for iterating through the loaded types.  It handles regular classes as well as interfaces, and the excludeSystemTypes option speeds things up hugely if you are looking for implementations in your own / third-party codebase.</p>\n\n<pre><code>public static List&lt;Type&gt; GetSubclassesOf(this Type type, bool excludeSystemTypes) {\n    List&lt;Type&gt; list = new List&lt;Type&gt;();\n    IEnumerator enumerator = Thread.GetDomain().GetAssemblies().GetEnumerator();\n    while (enumerator.MoveNext()) {\n        try {\n            Type[] types = ((Assembly) enumerator.Current).GetTypes();\n            if (!excludeSystemTypes || (excludeSystemTypes &amp;&amp; !((Assembly) enumerator.Current).FullName.StartsWith("System."))) {\n                IEnumerator enumerator2 = types.GetEnumerator();\n                while (enumerator2.MoveNext()) {\n                    Type current = (Type) enumerator2.Current;\n                    if (type.IsInterface) {\n                        if (current.GetInterface(type.FullName) != null) {\n                            list.Add(current);\n                        }\n                    } else if (current.IsSubclassOf(type)) {\n                        list.Add(current);\n                    }\n                }\n            }\n        } catch {\n        }\n    }\n    return list;\n}\n</code></pre>\n\n<p>It\'s not pretty, I\'ll admit.</p>\n'}, {'answer_id': 26754, 'author': 'Jorge Córdoba', 'author_id': 2695, 'author_profile': 'https://Stackoverflow.com/users/2695', 'pm_score': 2, 'selected': False, 'text': '<p>There\'s no easy way (in terms of performance) to do what you want to do.</p>\n\n<p>Reflection works with assemblys and types mainly so you\'ll have to get all the types of the assembly and query them for the right interface. Here\'s an example:</p>\n\n<pre><code>Assembly asm = Assembly.Load("MyAssembly");\nType[] types = asm.GetTypes();\nType[] result = types.where(x =&gt; x.GetInterface("IMyInterface") != null);\n</code></pre>\n\n<p>That will get you all the types that implement the IMyInterface in the Assembly MyAssembly</p>\n'}, {'answer_id': 26766, 'author': 'Ryan Rinaldi', 'author_id': 2278, 'author_profile': 'https://Stackoverflow.com/users/2278', 'pm_score': -1, 'selected': False, 'text': '<p>You could use some LINQ to get the list:</p>\n\n<pre><code>var types = from type in this.GetType().Assembly.GetTypes()\n            where type is ISomeInterface\n            select type;\n</code></pre>\n\n<p>But really, is that more readable?</p>\n'}, {'answer_id': 26768, 'author': 'Judah Gabriel Himango', 'author_id': 536, 'author_profile': 'https://Stackoverflow.com/users/536', 'pm_score': 6, 'selected': False, 'text': "<p>To find all types in an assembly that implement IFoo interface:</p>\n\n<pre><code>var results = from type in someAssembly.GetTypes()\n              where typeof(IFoo).IsAssignableFrom(type)\n              select type;\n</code></pre>\n\n<p>Note that Ryan Rinaldi's suggestion was incorrect. It will return 0 types. You cannot write</p>\n\n<pre><code>where type is IFoo\n</code></pre>\n\n<p>because type is a System.Type instance, and will never be of type IFoo. Instead, you check to see if IFoo is assignable from the type. That will get your expected results.</p>\n\n<p>Also, Adam Wright's suggestion, which is currently marked as the answer, is incorrect as well, and for the same reason. At runtime, you'll see 0 types come back, because all System.Type instances weren't IFoo implementors.</p>\n"}, {'answer_id': 4165635, 'author': 'Carl Nayak', 'author_id': 505851, 'author_profile': 'https://Stackoverflow.com/users/505851', 'pm_score': 4, 'selected': False, 'text': '<p>This worked for me (if you wish you could exclude system types in the lookup):</p>\n\n<pre><code>Type lookupType = typeof (IMenuItem);\nIEnumerable&lt;Type&gt; lookupTypes = GetType().Assembly.GetTypes().Where(\n        t =&gt; lookupType.IsAssignableFrom(t) &amp;&amp; !t.IsInterface); \n</code></pre>\n'}, {'answer_id': 10947832, 'author': 'hillstuk', 'author_id': 1298296, 'author_profile': 'https://Stackoverflow.com/users/1298296', 'pm_score': 5, 'selected': False, 'text': '<p>Other answers here use <code>IsAssignableFrom</code>.  You can also use <code>FindInterfaces</code> from the <code>System</code> namespace, as described <a href="http://msdn.microsoft.com/en-us/library/system.type.findinterfaces%28v=vs.100%29.aspx" rel="noreferrer">here</a>.</p>\n\n<p>Here\'s an example that checks all assemblies in the currently executing assembly\'s folder, looking for classes that implement a certain interface (avoiding LINQ for clarity).</p>\n\n<pre><code>static void Main() {\n    const string qualifiedInterfaceName = "Interfaces.IMyInterface";\n    var interfaceFilter = new TypeFilter(InterfaceFilter);\n    var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);\n    var di = new DirectoryInfo(path);\n    foreach (var file in di.GetFiles("*.dll")) {\n        try {\n            var nextAssembly = Assembly.ReflectionOnlyLoadFrom(file.FullName);\n            foreach (var type in nextAssembly.GetTypes()) {\n                var myInterfaces = type.FindInterfaces(interfaceFilter, qualifiedInterfaceName);\n                if (myInterfaces.Length &gt; 0) {\n                    // This class implements the interface\n                }\n            }\n        } catch (BadImageFormatException) {\n            // Not a .net assembly  - ignore\n        }\n    }\n}\n\npublic static bool InterfaceFilter(Type typeObj, Object criteriaObj) {\n    return typeObj.ToString() == criteriaObj.ToString();\n}\n</code></pre>\n\n<p>You can set up a list of interfaces if you want to match more than one.</p>\n'}, {'answer_id': 12602220, 'author': 'Ben Watkins', 'author_id': 1700301, 'author_profile': 'https://Stackoverflow.com/users/1700301', 'pm_score': 6, 'selected': False, 'text': '<p>This worked for me. It loops though the classes and checks to see if they are derrived from myInterface</p>\n\n<pre><code> foreach (Type mytype in System.Reflection.Assembly.GetExecutingAssembly().GetTypes()\n                 .Where(mytype =&gt; mytype .GetInterfaces().Contains(typeof(myInterface)))) {\n    //do stuff\n }\n</code></pre>\n'}, {'answer_id': 29379834, 'author': 'rism', 'author_id': 70149, 'author_profile': 'https://Stackoverflow.com/users/70149', 'pm_score': 6, 'selected': False, 'text': '<p>I appreciate this is a very old question but I thought I would add another answer for future users as all the answers to date use some form of <a href="https://msdn.microsoft.com/en-us/library/system.reflection.assembly.gettypes%28v=vs.110%29.aspx" rel="noreferrer"><code>Assembly.GetTypes</code></a>. </p>\n\n<p>Whilst GetTypes() will indeed return all types, it does not necessarily mean you could activate them and could thus potentially throw a <a href="https://msdn.microsoft.com/en-us/library/system.reflection.reflectiontypeloadexception%28v=vs.110%29.aspx" rel="noreferrer"><code>ReflectionTypeLoadException</code></a>.</p>\n\n<p>A classic example for not being able to activate a type would be when the type returned is <code>derived</code> from <code>base</code> but <code>base</code> is defined in a different assembly from that of <code>derived</code>, an assembly that the calling assembly does not reference. </p>\n\n<p>So say we have:</p>\n\n<pre><code>Class A // in AssemblyA\nClass B : Class A, IMyInterface // in AssemblyB\nClass C // in AssemblyC which references AssemblyB but not AssemblyA\n</code></pre>\n\n<p>If in <code>ClassC</code> which is in <code>AssemblyC</code> we then do something as per accepted answer:</p>\n\n<pre><code>var type = typeof(IMyInterface);\nvar types = AppDomain.CurrentDomain.GetAssemblies()\n    .SelectMany(s =&gt; s.GetTypes())\n    .Where(p =&gt; type.IsAssignableFrom(p));\n</code></pre>\n\n<p>Then it will throw a <a href="https://msdn.microsoft.com/en-us/library/system.reflection.reflectiontypeloadexception%28v=vs.110%29.aspx" rel="noreferrer"><code>ReflectionTypeLoadException</code></a>. </p>\n\n<p>This is because without a reference to <code>AssemblyA</code>  in <code>AssemblyC</code> you would not be able to:</p>\n\n<pre><code>var bType = typeof(ClassB);\nvar bClass = (ClassB)Activator.CreateInstance(bType);\n</code></pre>\n\n<p>In other words <code>ClassB</code> is not <em>loadable</em> which is something that the call to GetTypes checks and throws on.</p>\n\n<p>So to safely qualify the result set for loadable types then as per this <a href="http://haacked.com/" rel="noreferrer">Phil Haacked</a> article <a href="http://haacked.com/archive/2012/07/23/get-all-types-in-an-assembly.aspx/" rel="noreferrer">Get All Types in an Assembly</a> and <a href="https://stackoverflow.com/questions/7889228/how-to-prevent-reflectiontypeloadexception-when-calling-assembly-gettypes">Jon Skeet code</a> you would instead do something like:</p>\n\n<pre><code>public static class TypeLoaderExtensions {\n    public static IEnumerable&lt;Type&gt; GetLoadableTypes(this Assembly assembly) {\n        if (assembly == null) throw new ArgumentNullException("assembly");\n        try {\n            return assembly.GetTypes();\n        } catch (ReflectionTypeLoadException e) {\n            return e.Types.Where(t =&gt; t != null);\n        }\n    }\n}\n</code></pre>\n\n<p>And then:</p>\n\n<pre><code>private IEnumerable&lt;Type&gt; GetTypesWithInterface(Assembly asm) {\n    var it = typeof (IMyInterface);\n    return asm.GetLoadableTypes().Where(it.IsAssignableFrom).ToList();\n}\n</code></pre>\n'}, {'answer_id': 49006805, 'author': 'akop', 'author_id': 6537157, 'author_profile': 'https://Stackoverflow.com/users/6537157', 'pm_score': 0, 'selected': False, 'text': '<p>I got exceptions in the linq-code so I do it this way (without a complicated extension):</p>\n\n<pre><code>private static IList&lt;Type&gt; loadAllImplementingTypes(Type[] interfaces)\n{\n    IList&lt;Type&gt; implementingTypes = new List&lt;Type&gt;();\n\n    // find all types\n    foreach (var interfaceType in interfaces)\n        foreach (var currentAsm in AppDomain.CurrentDomain.GetAssemblies())\n            try\n            {\n                foreach (var currentType in currentAsm.GetTypes())\n                    if (interfaceType.IsAssignableFrom(currentType) &amp;&amp; currentType.IsClass &amp;&amp; !currentType.IsAbstract)\n                        implementingTypes.Add(currentType);\n            }\n            catch { }\n\n    return implementingTypes;\n}\n</code></pre>\n'}, {'answer_id': 52411210, 'author': 'Antonin GAVREL', 'author_id': 3161139, 'author_profile': 'https://Stackoverflow.com/users/3161139', 'pm_score': 3, 'selected': False, 'text': '<p>Other answer were not working with a <strong>generic interface</strong>.</p>\n\n<p>This one does, just replace typeof(ISomeInterface) by typeof (T).</p>\n\n<pre><code>List&lt;string&gt; types = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x =&gt; x.GetTypes())\n            .Where(x =&gt; typeof(ISomeInterface).IsAssignableFrom(x) &amp;&amp; !x.IsInterface &amp;&amp; !x.IsAbstract)\n            .Select(x =&gt; x.Name).ToList();\n</code></pre>\n\n<p>So with</p>\n\n<pre><code>AppDomain.CurrentDomain.GetAssemblies().SelectMany(x =&gt; x.GetTypes())\n</code></pre>\n\n<p>we get all the assemblies</p>\n\n<pre><code>!x.IsInterface &amp;&amp; !x.IsAbstract\n</code></pre>\n\n<p>is used to exclude the interface and abstract ones and</p>\n\n<pre><code>.Select(x =&gt; x.Name).ToList();\n</code></pre>\n\n<p>to have them in a list.</p>\n'}, {'answer_id': 54297288, 'author': 'user489566', 'author_id': 10558750, 'author_profile': 'https://Stackoverflow.com/users/10558750', 'pm_score': 2, 'selected': False, 'text': '<p>Even better when choosing the Assembly location. Filter most of the assemblies if you know all your implemented interfaces are within the same Assembly.DefinedTypes.</p>\n\n<pre><code>// We get the assembly through the base class\nvar baseAssembly = typeof(baseClass).GetTypeInfo().Assembly;\n\n// we filter the defined classes according to the interfaces they implement\nvar typeList = baseAssembly.DefinedTypes.Where(type =&gt; type.ImplementedInterfaces.Any(inter =&gt; inter == typeof(IMyInterface))).ToList();\n</code></pre>\n\n<p><a href="https://social.msdn.microsoft.com/Forums/en-US/abf6e015-3ece-4eab-85c9-d1878a5135e6/reflection-getting-all-classes-that-implement-a-given-interface?forum=winappswithcsharp" rel="nofollow noreferrer">By Can Bilgin</a></p>\n'}, {'answer_id': 57359586, 'author': 'Jonathan Santiago', 'author_id': 4457506, 'author_profile': 'https://Stackoverflow.com/users/4457506', 'pm_score': -1, 'selected': False, 'text': '<pre><code>   public IList&lt;T&gt; GetClassByType&lt;T&gt;()\n   {\n        return AppDomain.CurrentDomain.GetAssemblies()\n                          .SelectMany(s =&gt; s.GetTypes())\n                          .ToList(p =&gt; typeof(T)\n                          .IsAssignableFrom(p) &amp;&amp; !p.IsAbstract &amp;&amp; !p.IsInterface)\n                          .SelectList(c =&gt; (T)Activator.CreateInstance(c));\n   }\n</code></pre>\n'}, {'answer_id': 60841252, 'author': 'Hamdi Baligh', 'author_id': 1745172, 'author_profile': 'https://Stackoverflow.com/users/1745172', 'pm_score': 0, 'selected': False, 'text': '<p>OfType Linq method can be used exactly for this kind of scenarios:</p>\n\n<p><a href="https://learn.microsoft.com/fr-fr/dotnet/api/system.linq.enumerable.oftype?view=netframework-4.8" rel="nofollow noreferrer">https://learn.microsoft.com/fr-fr/dotnet/api/system.linq.enumerable.oftype?view=netframework-4.8</a></p>\n'}, {'answer_id': 60949971, 'author': 'diegosasw', 'author_id': 2948212, 'author_profile': 'https://Stackoverflow.com/users/2948212', 'pm_score': 1, 'selected': False, 'text': "<p>There are many valid answers already but I'd like to add anther implementation as a Type extension and a list of unit tests to demonstrate different scenarios:</p>\n\n<pre><code>public static class TypeExtensions\n{\n    public static IEnumerable&lt;Type&gt; GetAllTypes(this Type type)\n    {\n        var typeInfo = type.GetTypeInfo();\n        var allTypes = GetAllImplementedTypes(type).Concat(typeInfo.ImplementedInterfaces);\n        return allTypes;\n    }\n\n    private static IEnumerable&lt;Type&gt; GetAllImplementedTypes(Type type)\n    {\n        yield return type;\n        var typeInfo = type.GetTypeInfo();\n        var baseType = typeInfo.BaseType;\n        if (baseType != null)\n        {\n            foreach (var foundType in GetAllImplementedTypes(baseType))\n            {\n                yield return foundType;\n            }\n        }\n    }\n}\n</code></pre>\n\n<p>This algorithm supports the following scenarios:</p>\n\n<pre><code>public static class GetAllTypesTests\n{\n    public class Given_A_Sample_Standalone_Class_Type_When_Getting_All_Types\n        : Given_When_Then_Test\n    {\n        private Type _sut;\n        private IEnumerable&lt;Type&gt; _expectedTypes;\n        private IEnumerable&lt;Type&gt; _result;\n\n        protected override void Given()\n        {\n            _sut = typeof(SampleStandalone);\n\n            _expectedTypes =\n                new List&lt;Type&gt;\n                {\n                    typeof(SampleStandalone),\n                    typeof(object)\n                };\n        }\n\n        protected override void When()\n        {\n            _result = _sut.GetAllTypes();\n        }\n\n        [Fact]\n        public void Then_It_Should_Return_The_Right_Type()\n        {\n            _result.Should().BeEquivalentTo(_expectedTypes);\n        }\n    }\n\n    public class Given_A_Sample_Abstract_Base_Class_Type_When_Getting_All_Types\n        : Given_When_Then_Test\n    {\n        private Type _sut;\n        private IEnumerable&lt;Type&gt; _expectedTypes;\n        private IEnumerable&lt;Type&gt; _result;\n\n        protected override void Given()\n        {\n            _sut = typeof(SampleBase);\n\n            _expectedTypes =\n                new List&lt;Type&gt;\n                {\n                    typeof(SampleBase),\n                    typeof(object)\n                };\n        }\n\n        protected override void When()\n        {\n            _result = _sut.GetAllTypes();\n        }\n\n        [Fact]\n        public void Then_It_Should_Return_The_Right_Type()\n        {\n            _result.Should().BeEquivalentTo(_expectedTypes);\n        }\n    }\n\n    public class Given_A_Sample_Child_Class_Type_When_Getting_All_Types\n        : Given_When_Then_Test\n    {\n        private Type _sut;\n        private IEnumerable&lt;Type&gt; _expectedTypes;\n        private IEnumerable&lt;Type&gt; _result;\n\n        protected override void Given()\n        {\n            _sut = typeof(SampleChild);\n\n            _expectedTypes =\n                new List&lt;Type&gt;\n                {\n                    typeof(SampleChild),\n                    typeof(SampleBase),\n                    typeof(object)\n                };\n        }\n\n        protected override void When()\n        {\n            _result = _sut.GetAllTypes();\n        }\n\n        [Fact]\n        public void Then_It_Should_Return_The_Right_Type()\n        {\n            _result.Should().BeEquivalentTo(_expectedTypes);\n        }\n    }\n\n    public class Given_A_Sample_Base_Interface_Type_When_Getting_All_Types\n        : Given_When_Then_Test\n    {\n        private Type _sut;\n        private IEnumerable&lt;Type&gt; _expectedTypes;\n        private IEnumerable&lt;Type&gt; _result;\n\n        protected override void Given()\n        {\n            _sut = typeof(ISampleBase);\n\n            _expectedTypes =\n                new List&lt;Type&gt;\n                {\n                    typeof(ISampleBase)\n                };\n        }\n\n        protected override void When()\n        {\n            _result = _sut.GetAllTypes();\n        }\n\n        [Fact]\n        public void Then_It_Should_Return_The_Right_Type()\n        {\n            _result.Should().BeEquivalentTo(_expectedTypes);\n        }\n    }\n\n    public class Given_A_Sample_Child_Interface_Type_When_Getting_All_Types\n        : Given_When_Then_Test\n    {\n        private Type _sut;\n        private IEnumerable&lt;Type&gt; _expectedTypes;\n        private IEnumerable&lt;Type&gt; _result;\n\n        protected override void Given()\n        {\n            _sut = typeof(ISampleChild);\n\n            _expectedTypes =\n                new List&lt;Type&gt;\n                {\n                    typeof(ISampleBase),\n                    typeof(ISampleChild)\n                };\n        }\n\n        protected override void When()\n        {\n            _result = _sut.GetAllTypes();\n        }\n\n        [Fact]\n        public void Then_It_Should_Return_The_Right_Type()\n        {\n            _result.Should().BeEquivalentTo(_expectedTypes);\n        }\n    }\n\n    public class Given_A_Sample_Implementation_Class_Type_When_Getting_All_Types\n        : Given_When_Then_Test\n    {\n        private Type _sut;\n        private IEnumerable&lt;Type&gt; _expectedTypes;\n        private IEnumerable&lt;Type&gt; _result;\n\n        protected override void Given()\n        {\n            _sut = typeof(SampleImplementation);\n\n            _expectedTypes =\n                new List&lt;Type&gt;\n                {\n                    typeof(SampleImplementation),\n                    typeof(SampleChild),\n                    typeof(SampleBase),\n                    typeof(ISampleChild),\n                    typeof(ISampleBase),\n                    typeof(object)\n                };\n        }\n\n        protected override void When()\n        {\n            _result = _sut.GetAllTypes();\n        }\n\n        [Fact]\n        public void Then_It_Should_Return_The_Right_Type()\n        {\n            _result.Should().BeEquivalentTo(_expectedTypes);\n        }\n    }\n\n    public class Given_A_Sample_Interface_Instance_Type_When_Getting_All_Types\n        : Given_When_Then_Test\n    {\n        private Type _sut;\n        private IEnumerable&lt;Type&gt; _expectedTypes;\n        private IEnumerable&lt;Type&gt; _result;\n\n        class Foo : ISampleChild { }\n\n        protected override void Given()\n        {\n            var foo = new Foo();\n            _sut = foo.GetType();\n\n            _expectedTypes =\n                new List&lt;Type&gt;\n                {\n                    typeof(Foo),\n                    typeof(ISampleChild),\n                    typeof(ISampleBase),\n                    typeof(object)\n                };\n        }\n\n        protected override void When()\n        {\n            _result = _sut.GetAllTypes();\n        }\n\n        [Fact]\n        public void Then_It_Should_Return_The_Right_Type()\n        {\n            _result.Should().BeEquivalentTo(_expectedTypes);\n        }\n    }\n\n    sealed class SampleStandalone { }\n    abstract class SampleBase { }\n    class SampleChild : SampleBase { }\n    interface ISampleBase { }\n    interface ISampleChild : ISampleBase { }\n    class SampleImplementation : SampleChild, ISampleChild { }\n}\n</code></pre>\n"}, {'answer_id': 63209158, 'author': 'rvnlord', 'author_id': 3783852, 'author_profile': 'https://Stackoverflow.com/users/3783852', 'pm_score': 3, 'selected': False, 'text': "<p>I see so many overcomplicated answers here and people always tell me that I tend to overcomplicate things. Also using <code>IsAssignableFrom</code> method for the purpose of solving OP problem is wrong!</p>\n<p>Here is my example, it selects all assemblies from the app domain, then it takes flat list of all available types and checks every single type's list of interfaces for match:</p>\n<pre><code>public static IEnumerable&lt;Type&gt; GetImplementingTypes(this Type itype) \n    =&gt; AppDomain.CurrentDomain.GetAssemblies().SelectMany(s =&gt; s.GetTypes())\n           .Where(t =&gt; t.GetInterfaces().Contains(itype));\n</code></pre>\n"}, {'answer_id': 66900943, 'author': 'chtenb', 'author_id': 1546844, 'author_profile': 'https://Stackoverflow.com/users/1546844', 'pm_score': 3, 'selected': False, 'text': '<p>All the answers posted thus far either take too few or too many assemblies into account. You need to only inspect the assemblies that reference the assembly containing the interface. This minimizes the number of static constructors being run unnecessarily and save a huge amount of time and possibly unexpected side effects in the case of third party assemblies.</p>\n<pre><code>public static class ReflectionUtils\n{\n    public static bool DoesTypeSupportInterface(Type type, Type inter)\n    {\n        if (inter.IsAssignableFrom(type))\n            return true;\n        if (type.GetInterfaces().Any(i =&gt; i.IsGenericType &amp;&amp; i.GetGenericTypeDefinition() == inter))\n            return true;\n        return false;\n    }\n\n    public static IEnumerable&lt;Assembly&gt; GetReferencingAssemblies(Assembly assembly)\n    {\n        return AppDomain\n            .CurrentDomain\n            .GetAssemblies().Where(asm =&gt; asm.GetReferencedAssemblies().Any(asmName =&gt; AssemblyName.ReferenceMatchesDefinition(asmName, assembly.GetName())));\n    }\n\n    public static IEnumerable&lt;Type&gt; TypesImplementingInterface(Type desiredType)\n    {\n        var assembliesToSearch = new Assembly[] { desiredType.Assembly }\n            .Concat(GetReferencingAssemblies(desiredType.Assembly));\n        return assembliesToSearch.SelectMany(assembly =&gt; assembly.GetTypes())\n            .Where(type =&gt; DoesTypeSupportInterface(type, desiredType));\n    }\n\n    public static IEnumerable&lt;Type&gt; NonAbstractTypesImplementingInterface(Type desiredType)\n    {\n        return TypesImplementingInterface(desiredType).Where(t =&gt; !t.IsAbstract);\n    }\n}\n</code></pre>\n'}, {'answer_id': 72022365, 'author': 'classicSchmosby98', 'author_id': 7368872, 'author_profile': 'https://Stackoverflow.com/users/7368872', 'pm_score': 0, 'selected': False, 'text': "<p>If it helps anyone, this is what I'm using to make some of my unit tests easier :)</p>\n<pre><code>public static Type GetInterfacesImplementation(this Type type)\n{\n    return type.Assembly.GetTypes()\n        .Where(p =&gt; type.IsAssignableFrom(p) &amp;&amp; !p.IsInterface)\n        .SingleOrDefault();\n}\n</code></pre>\n"}]
['https://Stackoverflow.com/questions/26733', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/1782/']