To retrieve a IEnuemerable of all types in the current or another Assembly that implement a specific Interface / Abstract class and so on, you can use the snippets below.

Methods

C# Version

Assembly myAssembly = Assembly.LoadFrom(@"C:\Codesnippets.Fesslersoft.de\Fesslersoft.dll");
            
//Gets all referenced Types of the current Assembly that implement a specific interface
IEnumerable<Type> currentAssemblytypes = AppDomain.CurrentDomain.GetAssemblies().SelectMany(x => x.GetTypes()).Where(y => typeof(MyInterfaceName).IsAssignableFrom(y) && !y.IsInterface);
            
//Gets all referenced Types of an assembly that implement a specific interface
IEnumerable<Type> otherAssemblyTypes = myAssembly.GetTypes().Where(y => typeof(MyAbractClassName).IsAssignableFrom(y) && !y.IsInterface);

//You can also change IsInterface to IsAbstract if you are checking for types that implement an Abstract Class.
IEnumerable<Type> otherAssemblyTypesAbstract = myAssembly.GetTypes().Where(y => typeof(MyAbractClassName).IsAssignableFrom(y) && !y.IsAbstract);

VB.NET Version

Dim myAssembly As Assembly = Assembly.LoadFrom("C:\Codesnippets.Fesslersoft.de\Fesslersoft.dll")

'Gets all referenced Types of the current Assembly that implement a specific interface
Dim currentAssemblytypes As IEnumerable(Of Type) = AppDomain.CurrentDomain.GetAssemblies().SelectMany(Function(x) x.GetTypes()).Where(Function(y) GetType(MyInterfaceName).IsAssignableFrom(y) AndAlso Not y.IsInterface)

'Gets all referenced Types of an assembly that implement a specific interface
Dim otherAssemblyTypes As IEnumerable(Of Type) = myAssembly.GetTypes().Where(Function(y) GetType(MyAbractClassName).IsAssignableFrom(y) AndAlso Not y.IsInterface)

'You can also change IsInterface to IsAbstract if you are checking for types that implement an Abstract Class.
 Dim otherAssemblyTypesAbstract As IEnumerable(Of Type) = myAssembly.GetTypes().Where(Function(y) GetType(MyAbractClassName).IsAssignableFrom(y) AndAlso Not y.IsAbstract)
Compatibility: working .NET 2.0 working .NET 3.0 not tested .NET 3.5 not working .NET 4.0 not working .NET 4.5

Do you have an alternate or better method for this task?
If you have any questions or suggestions feel free to rate this snippet, post a comment or Contact Us via Email.

Related links:

6 thought on “How to get all types that implement a specific Interface in C# and VB.NET”

Leave a Reply