views:

23

answers:

1

Hello,

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 :

ilSetterGen.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...

+1  A: 

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_*).

Marc Gravell
It is just what I have found 5 min ago :) Now I am looking for to know how to test the equality of two values...
Jmix90
@Jmix90 - you can hard-code a few that use "ceq", else op_Equality or .Equals. You can probably borrow sone code of mine if yo want (from protobuf-net v2 which does lots of emit); I'm still not at a PC but I can cite the files later if you want.
Marc Gravell
@Marc thx a lot !
Jmix90