I started rewriting my Function Plotter, which takes a math function and calculates the y value for a given x. To overwrite it, I want to dynamically create a method using IL. The IL test code I have now uses 2 LocalBuilders and multiplies them. However, when I return the value, I get (what seems) a random number instead of the real answer.
Here is the following code I used.
ILGenerator il = hello.GetILGenerator();
LocalBuilder a = il.DeclareLocal(typeof(int));
LocalBuilder b = il.DeclareLocal(typeof(int));
LocalBuilder multOfAandB = il.DeclareLocal(typeof(int));
il.Emit(OpCodes.Ldc_I4, 5);
il.Emit(OpCodes.Stloc, a);
il.Emit(OpCodes.Ldc_I4, 6);
il.Emit(OpCodes.Stloc, b);
il.Emit(OpCodes.Ldloc, a);
il.Emit(OpCodes.Ldloc, b);
il.Emit(OpCodes.Mul);
il.Emit(OpCodes.Ret);
This should return 30, but currently I'm getting 4.2038953929744512E-44. Is there something wrong with my code that causes the function to return the wrong value?
Thanks in advance
EDIT
The code calling the function is as follows:
object[] invokeArgs = { 42 };
object obj = func.helloMethod.Invoke(null, BindingFlags.ExactBinding, null, invokeArgs, new CultureInfo("en-us"));
, , func.helloMethod, DynamicMethod, :
DynamicMethod hello = new DynamicMethod("Hello",
typeof(double),
helloArgs,
typeof(double).Module);