문제

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);
라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top