1

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

Community
  • 1
  • 1
acostela
  • 2,597
  • 3
  • 33
  • 50
  • Sorry but I could not understand what is the problem you face? What exactly you mean by "I Can't call" ? – Siva Gopal Nov 10 '15 at 10:06
  • I get the error "can not find symbol "interfaceClass" – acostela Nov 10 '15 at 10:08
  • 1
    http://stackoverflow.com/questions/2107845/generics-in-c-using-type-of-a-variable-as-parameter I think your question already exists but not 100% as I'm not that much into generics – Thomas Nov 10 '15 at 10:18

2 Answers2

2

Lets say CreateManager<T> is a method of type Foo:

public class Foo
{
    private void CreateManager<T>(ServiceProvider serviceProvider,
                                  DeviceCapability deviceCapability)
    {
    }
}

In order to dynamically invoke the generic method, you'll need to get the MethodInfo first, then call MakeGenericMethod with the actual type you want to pass (I choose string for the example)

var foo = new Foo();
var createManagerMethod = foo.GetType()
                             .GetMethod("CreateManager", 
                                         BindingFlags.Instance | BindingFlags.NonPublic);

var method = createManagerMethod.MakeGenericMethod(typeof(string));
method.Invoke(foo, new object[] { new ServiceProvider(), new DeviceCapability() });

Finally, call Invoke with the proper object instance and parameters.

Yuval Itzchakov
  • 146,575
  • 32
  • 257
  • 321
  • Thank you for your response. But I'm getting the same error "Can not find symbol" when I use my "Type object". I think the problem is that in this way the types are resolved at compilertime and with reflection types are resolved at runtime. Maybe is this? – acostela Nov 10 '15 at 10:20
  • 1
    Ok it worked perfectly! I changed the string typeof with a dinamyc instance of my type and it worked "MakeGenericMethod(new Type[] { interfaceClass })" – acostela Nov 10 '15 at 10:28
0

I don't see, why you need reflection for this. You could do it like shown below

public interface IYourInterface //Give it a good name
{
    void Instantiate(ServiceProvider serviceProvider, DeviceCapability deviceCapability);
}

public class YourClass : IYourInterface
{
    public YourClass() 
    {
        // do something if you want
    }

    public void Instantiate (ServiceProvider serviceProvider, DeviceCapability deviceCapability)
    {
       // what ever needs to be done here
    }
}

private void CreateManager<T>(ServiceProvider serviceProvider, DeviceCapability deviceCapability) 
    where T : IYourInterface, new()
{
    var instance = new T();
    instance.Instantiate(serviceProvider, deviceCapability);
}

/*
   Usage
*/

    CreateManager<YourClass>(serviceProvider, deviceCapability);
DHN
  • 4,807
  • 3
  • 31
  • 45
  • 1
    I need reflection because I read the classes dinamically from an option file and after that I must add them to the serviceProvider in runtime. i.e. serviceProvider.Add(objectInstantiated); – acostela Nov 10 '15 at 10:10
  • Ok in that case, you cannot do it that way. ;o) – DHN Nov 10 '15 at 10:15
  • I finally did it. You can check the answer. Thank you for your response ;) – acostela Nov 10 '15 at 10:35
  • Thanks for keeping me up to date. Glad to hear that you could make it. – DHN Nov 11 '15 at 08:21