There are some definations:
public class Message
{
public SayType Say(string name)
{
Console.Write("Hello," + name );
return SayType.Name;
}
public SayType Say(string name,string haha)
{
Console.Write("Hello," + name );
return SayType.Name;
}
}
public enum SayType
{
Name
}
SayDelegate:
public delegate SayType SayDelegate(object message,params object[] o);
And I want to create two dynamic of the two function in class Message. The first:
DynamicMethod dynamicMethod =
new DynamicMethod("Say",typeof(SayType),new Type[]{typeof(object),typeof(object[])});
var il = dynamicMethod.GetILGenerator();
il.Emit(OpCodes.Ldarg,0);
il.Emit(OpCodes.Ldarg,1);
il.Emit(OpCodes.Ldc_I4,0);
il.Emit(OpCodes.Ldelem_Ref);
il.Emit(OpCodes.Callvirt,typeof(Message).GetMethods()[0]);
//[0] refers to the first method
il.Emit(OpCodes.Ret);
System.Delegate delegates = dynamicMethod.CreateDelegate(typeof(SayDelegate));
delegates.DynamicInvoke(new Message(), new object[]{"123123"});
It can work normally.
However, if I create the second delegate like this:
DynamicMethod dynamicMethod =
new DynamicMethod("Say",typeof(SayType),new Type[]{typeof(object),typeof(object[])});
var il = dynamicMethod.GetILGenerator();
il.Emit(OpCodes.Ldarg,0);
il.Emit(OpCodes.Ldarg,1);
il.Emit(OpCodes.Ldc_I4,0);
il.Emit(OpCodes.Ldelem_Ref);
il.Emit(OpCodes.Ldc_I4,1);
il.Emit(OpCodes.Ldelem_Ref);
il.Emit(OpCodes.Callvirt,typeof(Message).GetMethods()[1]);
//[1] refers to the second method
il.Emit(OpCodes.Ret);
System.Delegate delegates = dynamicMethod.CreateDelegate(typeof(SayDelegate));
delegates.DynamicInvoke(new Message(), new object[]{"123123","123123"});
It will have some complains:
---> System.InvalidProgramException: Common Language Runtime detected an invalid program.
at Say(Object , Object[] )
--- End of inner exception stack trace ---
You missed a
ldarg.1:The trick is to compile and decompile what you want, and look at the IL; in this case we see:
and compare that to your opcodes (noting that you're using unsafe coerce, which ... I'm not going to advocate for, but I also can't stop you from doing)