Given a generic type with constraints:
class MyClass<T> where T: Alpha
{
}
and implementations of that constraint:
class Alpha {}
class Bravo : Alpha {}
class Charlie : Alpha {}
How can get generic types for all the combinations at run-time?
// I want these types at run-time
MyClass<Alpha>
MyClass<Bravo>
MyClass<Charlie>
EDIT: Based on @rich.okelly's answer, I think the real question is this:
How can I find all the types that implement my generic type's constraint at run-time?
So if I'm given typeof(MyClass<>)
, I get the types above.
You can enumerate all loaded assemblies in the app domain. And then enumerate all the types in each Assembly and test to see if each type is a subclass of the constant.
Something like:
Alternatively, if the generic types have already been loaded:
Update
You can use the
GetGenericArguments()
method and theGetGenericParameterConstraints()
method to get the required types.