Say I have a method defined as follows:
public T InvokeMethod<T>(string serviceName, string methodName, params object[] args)
Say I want to invoke it with a Type object as follows, it doesn’t work:
int i = 100;
Type x = i.GetType();
invoker.InvokeMethod<x>(method.Item1, method.Item2, null);
I know I can invoke this method by actually specifying the type as follows, but I want it to be dynamic.
invoker.InvokeMethod<int>(method.Item1, method.Item2, null);
How can I accomplish this?
Because x isnt a type that you can use in a generic class, x is an instance of the type
Type. This is calculated at run time. Generic classes expect a class name, calculated at compile time. This is better explained in this answer:https://stackoverflow.com/a/7120375/
Fundamentally, x is an instance of a class, but in
invoker.InvokeMethod<int>,intis the name of a class.