Skip to content

Instantly share code, notes, and snippets.

@jeangatto
Created February 23, 2023 18:58
Show Gist options
  • Save jeangatto/e2c976a59a6bbc0f8d6524043b152132 to your computer and use it in GitHub Desktop.
Save jeangatto/e2c976a59a6bbc0f8d6524043b152132 to your computer and use it in GitHub Desktop.
C# Getting all types that implement an interface
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