Search code examples
c#mathcil

IL Return in C# returns wrong value


I started re-writing my Function Plotter which takes mathematical function and calculates a y value for a given x. To re-write it, I want to dynamically create a method using IL. The test IL code that I have now uses 2 LocalBuilders and multiplies them. However, when i return the value, i receive(what seems to be) a random number instead of the real answer.

Here's the following code that I've been using.

        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); // Store "5" ...
        il.Emit(OpCodes.Stloc, a);  // ... in "a".

        il.Emit(OpCodes.Ldc_I4, 6); // Store "6" ...
        il.Emit(OpCodes.Stloc, b);  // ... in "b".

        il.Emit(OpCodes.Ldloc, a);
        il.Emit(OpCodes.Ldloc, b); 

        il.Emit(OpCodes.Mul);       // Multiply them ...
        il.Emit(OpCodes.Ret);       // ... and return the result.

This should return 30 but currently I'm receiving 4.2038953929744512E-44. Is there something wrong with my code thats causing 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"));

and in my class where I store the function to later call it from func.helloMethod has a DynamicMethod which is defined as follows:

DynamicMethod hello = new DynamicMethod("Hello",
            typeof(double),
            helloArgs,
            typeof(double).Module);

Solution

  • You seem to accidentially coerce the return value from int to double. Dynamic execution is surprisingly fault-tolerant here, there seems to be no check for type mismatch.

    Change the calling code to match the data types of the internal locals:

    var hello = new DynamicMethod(
        "Hello",
        typeof(int),
        helloArgs,
        typeof(YourClassNameHere).Module
    );
    

    Note that the type in the last argument should be your class name, not that of the argument data type.