I am facing an issue with the reflection in C#. I need to construct a generic method that instantiate a class type dynamically with reflection. What I tried is the following.
Type myClass = Type.GetType(deviceBehavior.@class);
Type myInterfaceClass = myClass.GetInterface(deviceBehavior.@interface);
if (typeof (AnotherInterface).IsAssignableFrom(interfaceClass))
{
CreateManager<interfaceClass>(serviceProvider, deviceCapability);
}
My CreateManager method is as following:
private void CreateManager<T>(ServiceProvider serviceProvider, DeviceCapability deviceCapability)
{
T instanceToCreate = CreateClass<T>(serviceProvider, deviceCapability);
//Code to instantiate my class
}
The problem is that I can't call
CreateManager(serviceProvider, deviceCapability);
How can I pass an interface to my generic type? I searched and I couldn't find anything that I could understand clearly. i.e.
Calling a static method on a generic type parameter
Pass An Instantiated System.Type as a Type Parameter for a Generic Class