开发者

Operation could destabilize the runtime when emiting a nullable setter

开发者 https://www.devze.com 2023-01-20 15:51 出处:网络
I am currently overriding the setter of a given class with Reflection.Emit. Everything works fine except when I use it with a nullable property....

I am currently overriding the setter of a given class with Reflection.Emit. Everything works fine except when I use it with a nullable property....

Here is the code I use :

ilSet开发者_如何学JAVAterGen.Emit(OpCodes.Ldarg_0);
ilSetterGen.Emit(OpCodes.Call, baseGetter);
ilSetterGen.Emit(OpCodes.Ldarg_1);
ilSetterGen.Emit(OpCodes.Ceq);
Label retLabel = ilSetterGen.DefineLabel();
ilSetterGen.Emit(OpCodes.Brtrue_S, retLabel);



ilSetterGen.MarkLabel(retLabel);
ilSetterGen.Emit(OpCodes.Ret);

Do you have any clue ?

EDIT : as pointed out in the answer the problem is in the equality test... I so removed the irrelevants part...


As always, the first thing to do is to look at what similar code in c# generates as IL, for example via reflector.

I'm not at a PC but that "ceq" looks suspect; that only works for some primitives and references; a "lifted" equals would check HasValue of each, get the values of each, and use the appropriate equality test - possibly by "ceq", but possibly via static-call to the equality operator (op_*).

0

精彩评论

暂无评论...
验证码 换一张
取 消