I am playing around with DynamicMethod and aim to do the following:
I have an Action from which I obtain the IL code as bytes using GetILAsByteArray()
. From this bytes I would like to create a Dynamic method and execute is. Here an example of what I am trying to do:
class Program
{
static void Main(string[] args)
{
//Create action and execute
Action<string> myAction = s =>
{
Console.WriteLine("Hello " + s);
};
myAction("World");
//Get IL bytes
byte[] ilBytes = myAction.GetMethodInfo().GetMethodBody().GetILAsByteArray();
DynamicMethod dynamicCallback = new DynamicMethod("myAction", typeof(void), new Type[] { typeof(string) });
DynamicILInfo dynamicIlInfo = dynamicCallback.GetDynamicILInfo();
dynamicIlInfo.SetCode(ilBytes, 100);
dynamicCallback.Invoke(null, new object[] { "World" });
}
}
When calling a dynamicCallback.Invoke(null, new object[] { "World" })
we get "Exception thrown: 'System.BadImageFormatException' in mscorlib.dll".
One thing I have no idea abut is what I should use as second argument for SetCode()
, what should be used as 'maxStackSize'? How can I set the same value as for the initial action? But I suppose this is not the reason for the exception.
How can I properly create a dynamic method from the IL bytes?
Aucun commentaire:
Enregistrer un commentaire