Assuming I've got a class looking like the following one:
public class MyClass<MyType> : MyBaseClass<MyType> where MyType : class, IMyInterface
{
}
In a first step, I'm getting a List with all types of the classes that implement IMyInterface var typeIMyInterface = typeof(IMyInterface);
var typesThatImplementIMyInterface = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => typeIMyInterface .IsAssignableFrom(p) && p.IsClass);
Then in a second step, I want to loop through these types and create an instance of MyClass for the type in the loop fof further usage.
foreach (Type typeThatImplementsIMyInterface in typesThatImplementIMyInterface)
{
var myInstance = Activator.CreateInstance<MyClass<typeThatImplementsIMyInterface>>();
}
Unfortunately, the last block of code doesn't work because of a compile-time error "Cannot resolve type typeThatImplementsIMyInterface".
How can I pass this type correctly, so that I can successfully create an instance of the object?