Created
February 23, 2023 18:58
-
-
Save jeangatto/e2c976a59a6bbc0f8d6524043b152132 to your computer and use it in GitHub Desktop.
C# Getting all types that implement an interface
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var myInterfaceTypeOf = typeof(IReadDbMapping); | |
var implementations = AppDomain | |
.CurrentDomain | |
.GetAssemblies() | |
.SelectMany(assembly => assembly.GetTypes()) | |
.Where(type => myInterfaceTypeOf.IsAssignableFrom(type) | |
&& type.IsClass | |
&& !type.IsAbstract | |
&& !type.IsInterface) | |
.ToList(); | |
foreach (var type in implementations) | |
{ | |
var instance = (T)Activator.CreateInstance(type); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment