发出代码在运行时崩溃但在调试时不会崩溃,为什么

发布于 2024-11-24 16:20:01 字数 1476 浏览 3 评论 0原文

我有一些代码,可以从类型构建代理。它工作完美。 然后我在 setter 发出代码中添加,它必须在调用时推送 isDirty 位。这次失败了,为什么呢?

如果我运行没有 isDirty 位的代码,它就会起作用。 如果我使用 isDirty 位运行代码,它可以在调试中工作,但会在 Visual Studio 中启动反汇编窗口。 如果我使用 isDirty (无调试)运行代码,则程序崩溃(无响应),但是当我点击取消时,它开始工作并显示所有正确的数据。

        PropertyBuilder property = proxy.DefineProperty(propertyInfo.Name, propertyInfo.Attributes, propertyInfo.PropertyType, null);
        MethodAttributes attributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.Virtual;
        MethodBuilder setMethod = proxy.DefineMethod("set_" + propertyInfo.Name, attributes, typeof(void), new Type[] { propertyInfo.PropertyType });

        ILGenerator setIL = setMethod.GetILGenerator();
        setIL.Emit(OpCodes.Ldarg_0); // load this on the stack - where this is the type we are creating
        setIL.Emit(OpCodes.Ldarg_1); // load first parameter on the stack
        setIL.Emit(OpCodes.Call, propertyInfo.GetSetMethod());

        {
        //error here: when this is uncomment, it fails 
        //    // set the isDirty bit
        //    setIL.Emit(OpCodes.Ldarg_0); // load this on the stack
        //    setIL.Emit(OpCodes.Ldc_I4_1, 1); // push a number on the stack 
        //    setIL.Emit(OpCodes.Stfld, isDirtyField); // save the value on the stack in field
        }

        setIL.Emit(OpCodes.Ret);

        property.SetSetMethod(setMethod);

我很难理解为什么会失败?需要专家的帮助:)

// 丹尼斯

I have some code, that build up a proxy from a type. It work perfekt.
Then I have add in the setter emit code, that it has to push a isDirty bit, when it is call. This fail, why?

If I run the code without the isDirty bit, it works.
If I run the code with the isDirty bit, it works in debug, but is start the disassembly window up in visual studio.
If I run the code with the isDirty (without-debug) the program crash (not responding) but when I hit cancel, it starts working and show all de rigth data.

        PropertyBuilder property = proxy.DefineProperty(propertyInfo.Name, propertyInfo.Attributes, propertyInfo.PropertyType, null);
        MethodAttributes attributes = MethodAttributes.Public | MethodAttributes.HideBySig | MethodAttributes.SpecialName | MethodAttributes.Virtual;
        MethodBuilder setMethod = proxy.DefineMethod("set_" + propertyInfo.Name, attributes, typeof(void), new Type[] { propertyInfo.PropertyType });

        ILGenerator setIL = setMethod.GetILGenerator();
        setIL.Emit(OpCodes.Ldarg_0); // load this on the stack - where this is the type we are creating
        setIL.Emit(OpCodes.Ldarg_1); // load first parameter on the stack
        setIL.Emit(OpCodes.Call, propertyInfo.GetSetMethod());

        {
        //error here: when this is uncomment, it fails 
        //    // set the isDirty bit
        //    setIL.Emit(OpCodes.Ldarg_0); // load this on the stack
        //    setIL.Emit(OpCodes.Ldc_I4_1, 1); // push a number on the stack 
        //    setIL.Emit(OpCodes.Stfld, isDirtyField); // save the value on the stack in field
        }

        setIL.Emit(OpCodes.Ret);

        property.SetSetMethod(setMethod);

I have a hard time, seeing why this fails? Need some help from the experts :)

// dennis

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

铃予 2024-12-01 16:20:01

我不确定这是否是您唯一的问题,但您在发出 Ldc_I4_1 操作码时使用了错误的 Emit 重载。您应该执行以下任一操作:

setIL.Emit(OpCodes.Ldc_I4_1)

setIL.Emit(OpCodes.Ldc_I4, 1)

第一个选项将导致稍小的 IL 方法主体,因为它使用专门的操作码,而第二个选项并不特定于正在加载的数字。

I'm not sure if this is your only issue, but you are using the wrong Emit overload when emitting your Ldc_I4_1 opcode. You should do either:

setIL.Emit(OpCodes.Ldc_I4_1)

or

setIL.Emit(OpCodes.Ldc_I4, 1)

The first option will result in a slightly smaller IL method body since it uses a specialized opcode, whereas the second one is not specific to the number being loaded.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文