I'm not sure, but based on all the components that you have in your question, I suspect you're looking for something similar:
using System;
public class Program { public static void Main() { var handlerType = typeof (SampleHandler1); var genericType = handlerType.GetInterface("IConsume`1"); var genericArguments = genericType.GetGenericArguments(); var consumeMethod = genericType.GetMethod("Consume"); var handlerConstructorInfo = handlerType.GetConstructor(Type.EmptyTypes); var handler = handlerConstructorInfo.Invoke(new object[] {}); var messageConstructorInfo = genericArguments[0].GetConstructor(Type.EmptyTypes); var messageObject = messageConstructorInfo.Invoke(new object[] {}); ((IBaseMessage)messageObject).Name = "Sample Message"; var argsx = new object[] {messageObject}; consumeMethod.Invoke(handler, argsx); } } public interface IConsume<in T> where T : class, IBaseMessage { void Consume(T message); } public class SampleHandler1 : IConsume<SampleMessage> { public SampleHandler1() { Console.WriteLine("SampleHandler1 constructed"); } public void Consume(SampleMessage message) { Console.WriteLine("Message consume: " + message.Name); } } public interface IBaseMessage { string Name { get; set; } } public class SampleMessage : IBaseMessage { public string Name { get; set; } }
Here is a working Dotnetfiddle from the answer above: https://dotnetfiddle.net/YFmmzk
Console output:
SampleHandler1 built
Message Consumption: Message Example
It looks like you are confusing your handler and message types. You tried to pass an instance of the handler itself to the consumption method. Moreover, IBaseMessage was missing from the declaration of the Name property.
UPDATE
Below is a cleaned version of this answer:
public class Program { public static void Main() { var handler = new DynamicConstructor(typeof (SampleHandler1)).New(); invokeIConsumeFor(handler, "Sample Message"); } private static void invokeIConsumeFor(object handler, string message) { var executer = new DynamicGenericInterfaceExecuter(handler, "IConsume`1"); var messageObject = executer.GetTypeArgumentConstructor(0, Type.EmptyTypes).New(); ((IBaseMessage) messageObject).Name = message; executer.Method("Consume", messageObject.GetType()).Call(messageObject); } } public class DynamicGenericInterfaceExecuter { private object instance; private Type genericInterfaceFromType; private Type[] genericTypeArguments; public DynamicGenericInterfaceExecuter(object instance, string interfaceName) { this.instance = instance; this.genericInterfaceFromType = instance.GetType().GetInterface(interfaceName); this.genericTypeArguments = this.genericInterfaceFromType.GetGenericArguments(); } public MethodExecuter Method(string methodName, params Type[] parameterTypes) { return new MethodExecuter(this.instance, this.genericInterfaceFromType, methodName, parameterTypes); } public DynamicConstructor GetTypeArgumentConstructor(int typeArgumentIndex, params Type[] constructorParameterTypes) { return new DynamicConstructor(this.genericTypeArguments[typeArgumentIndex], constructorParameterTypes); } } public class DynamicConstructor { private System.Reflection.ConstructorInfo constructor; public DynamicConstructor(Type type, params Type[] constructorParameters) { this.constructor = type.GetConstructor(constructorParameters); } public object New(params object[] constructorArguments) { return this.constructor.Invoke(constructorArguments); } } public class MethodExecuter { private object instance; private System.Reflection.MethodInfo method; public MethodExecuter(object instance, Type containerType, string methodName, Type[] methodParameters) { this.instance = instance; this.method = containerType.GetMethod(methodName, methodParameters); } public void Call(params object[] arguments) { this.Invoke(arguments); } public object Invoke(params object[] arguments) { return this.method.Invoke(instance, arguments); } } public interface IConsume<in T> where T : class, IBaseMessage { void Consume(T message); } public class SampleHandler1 : IConsume<SampleMessage> { public SampleHandler1() { Console.WriteLine("SampleHandler1 constructed"); } public void Consume(SampleMessage message) { Console.WriteLine("Message consume: " + message.Name); } } public interface IBaseMessage { string Name { get; set; } } public class SampleMessage : IBaseMessage { public string Name { get; set; } }
And dotnetfiddle: https://dotnetfiddle.net/n9WHZ2
Keep in mind that this is not type-related in the least, but this does not seem to cause concern in your question.