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)
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:
- Assembly Class
- AppDomain.GetAssemblies Method
- Assembly.GetTypes Method
- Type.IsInterface Property
- Type.IsAbstract Property
RT @CodeSnippetsNET: Get all types that implement a specific Interface in C# and #VB .NET http://t.co/XZguPlnFgb #csharp #dotnet #coding #p…
RT @CodeSnippetsNET: Get all types that implement a specific Interface in C# and #VB .NET http://t.co/XZguPlnFgb #csharp #dotnet #coding #p…
RT @CodeSnippetsNET: Get all types that implement a specific Interface in C# and #VB .NET http://t.co/XZguPlnFgb #csharp #dotnet #coding #p…
RT @CodeSnippetsNET: Get all types that implement a specific Interface in C# and #VB .NET http://t.co/XZguPlnFgb #csharp #dotnet #coding #p…
RT @CodeSnippetsNET: Get all types that implement a specific Interface in C# and #VB .NET http://t.co/XZguPlnFgb #csharp #dotnet #coding #p…
This is the solution I was looking for, thx