0

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?

4

1 回答 1

4

如果你只是 emit ret,那就像return;在 C# 中一样。但是你需要return null;

你应该使用

il.Emit(OpCodes.Ldnull);
il.Emit(OpCodes.Ret);
于 2014-05-05T19:48:43.000 回答