当我尝试使用DynamicMethod创建始终返回true的方法时出错

时间:2022-02-12 15:26:57

Today, I started learning about the DynamicMethod class. For learning purposes, I set about to use DynamicMethod to create a function that takes no argument and always returns the boolean value true.

今天,我开始学习DynamicMethod类。出于学习目的,我开始使用DynamicMethod创建一个不带参数的函数,并始终返回布尔值true。

I created a function to do this in C# and then examined the resulting IL code with Telerik JustDecompile.

我创建了一个在C#中执行此操作的函数,然后使用Telerik JustDecompile检查生成的IL代码。

.method public hidebysig instance bool ReturnTrue1 () cil managed 
{
    .locals init (
        [0] bool CS$1$0000
    )

    IL_0000: nop
    IL_0001: ldc.i4.1
    IL_0002: stloc.0
    IL_0003: br.s IL_0005

    IL_0005: ldloc.0
    IL_0006: ret
}

It looks simple enough. According to the documentation, it looks like these instructions simply place an integer 1 on the stack to be returned.

看起来很简单。根据文档,看起来这些指令只是在要返回的堆栈上放置一个整数1。

Following along with some of the examples I've looked at, I wrote the following Console Application.

继我看过的一些例子之后,我编写了以下控制台应用程序。

using System;
using System.Reflection.Emit;

namespace EntityFrameworkDynamicMethod
{
    class Program
    {
        static void Main(string[] args)
        {            
            ReturnTrue ReturnTrueDelegate = GetReturnTrueDelegate();
            ReturnTrueDelegate();            
        }        

        delegate bool ReturnTrue();

        static ReturnTrue GetReturnTrueDelegate()
        {            
            DynamicMethod method = new DynamicMethod("ReturnTrue", typeof(bool), new Type[] {});
            ILGenerator generator = method.GetILGenerator();
            Label IL_0005 = generator.DefineLabel();
            generator.Emit(OpCodes.Nop);
            generator.Emit(OpCodes.Ldc_I4_1);
            generator.Emit(OpCodes.Stloc_0);            
            generator.Emit(OpCodes.Ldloc_0, IL_0005);            
            generator.MarkLabel(IL_0005);
            generator.Emit(OpCodes.Ret);

            return (ReturnTrue)method.CreateDelegate(typeof(ReturnTrue));
        }
    }
}

However, when I run this code, the following exception is raised on ReturnTrueDelegate();

但是,当我运行此代码时,在ReturnTrueDelegate()上引发以下异常;

System.Security.VerificationException: Operation could destabilize the runtime.
at ReturnTrue()

What does this exception mean and what do I do to fix this?

这个例外是什么意思,我该怎么做才能解决这个问题?

1 个解决方案

#1


4  

generator.Emit(OpCodes.Ldloc_0, IL_0005);

This is incorrect; the ldloc.0 instruction has no arguments (did you mean br.s?).
You also cannot use local 0 without declaring it.

这是不正确的; ldloc.0指令没有参数(你的意思是br.s?)。您也无法在不声明的情况下使用本地0。

However, you don't need any of that; all you need to do is load 1 (ldc.i4.1) and return it (ret).
If you decompile release-mode code, you should see that.

但是,你不需要任何这些;你需要做的只是加载1(ldc.i4.1)并返回它(ret)。如果你反编译发布模式代码,你应该看到。

#1


4  

generator.Emit(OpCodes.Ldloc_0, IL_0005);

This is incorrect; the ldloc.0 instruction has no arguments (did you mean br.s?).
You also cannot use local 0 without declaring it.

这是不正确的; ldloc.0指令没有参数(你的意思是br.s?)。您也无法在不声明的情况下使用本地0。

However, you don't need any of that; all you need to do is load 1 (ldc.i4.1) and return it (ret).
If you decompile release-mode code, you should see that.

但是,你不需要任何这些;你需要做的只是加载1(ldc.i4.1)并返回它(ret)。如果你反编译发布模式代码,你应该看到。