Call Generic method using runtime type and cast return object
- by markpirvine
I'm using reflection to call a generic method with a type determined at runtime. My code is as follows:
Type tType = Type.GetType(pLoadOut.Type);
MethodInfo method = typeof(ApiSerialiseHelper).GetMethod("Deserialise", new Type[] { typeof(string) });
MethodInfo generic = method.MakeGenericMethod(tType);
generic.Invoke(obj, new object[] { pLoadOut.Data });
This works ok. However the generic.Invoke method returns an object, but what I would like is the type determined at runtime. Is this possible with this approach, or is there a better option?
Mark