简体   繁体   English

IL返回C#返回错误的值

[英]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. 我开始重写我的函数绘图仪,它接受数学函数并计算给定x的ay值。 To re-write it, I want to dynamically create a method using IL. 要重新编写它,我想使用IL动态创建一个方法。 The test IL code that I have now uses 2 LocalBuilders and multiplies them. 我现在使用的测试IL代码使用2个LocalBuilders并将它们相乘。 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. 这应该返回30,但目前我收到了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: 在我的类中,我存储函数以便稍后从func.helloMethod调用它有一个DynamicMethod,定义如下:

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

You seem to accidentially coerce the return value from int to double . 您似乎意外地将返回值从int强制转换为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的定义:

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. 由于6 * 5返回30,这是一个int,因此在将int更改为double并返回错误答案时遇到了问题。 Once I changed it to typeof(int), the returned value was 30. I had no idea that IL was so particular on the type. 一旦我将其更改为typeof(int),返回的值为30.我不知道IL在类型上如此特别。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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