I've been using the FastMember project. It contains this code:
il.Emit(OpCodes.Ldarg, 2);
il.Emit(OpCodes.Newobj, typeof(ArgumentOutOfRangeException).GetConstructor(new[] { typeof(string) }));
il.Emit(OpCodes.Throw);
I would like to change that to just return null instead. I tried replacing it with a single line il.Emit(OpCodes.Ret);
. However, I get invalid program errors using that. How do I set the return value to null using emitted code?
If you just emit ret
, that's like return;
in C#. But you need return null;
You should use
il.Emit(OpCodes.Ldnull);
il.Emit(OpCodes.Ret);