简体   繁体   中英

IL Return in C# returns wrong value

I started re-writing my Function Plotter which takes mathematical function and calculates ay 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);

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.

The problem here was the definition of the DynamicMethod:

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

Since 6 * 5 returns 30, which is an int, it had problems changing the int to a double and returned the wrong answer. Once I changed it to typeof(int), the returned value was 30. I had no idea that IL was so particular on the type.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM