【发布时间】:2018-11-12 23:48:45
【问题描述】:
我想 IL 生成一个动态方法
delegate ArraySegment<byte> X(MyClass mc);
调用mc上的签名方法并返回其out参数。
MethodInfo methInf = aClass.GetMethod("Y",
BindingFlags.Public | BindingFlags.Instance,
null, new[] { typeof(ArraySegment<byte>).MakeByRefType() }, null);
但我不知道如何处理 out 参数。这是我到目前为止的代码。
DynamicMethod dm = new DynamicMethod("X", typeof(ArraySegment<byte>),
new[] { typeof(MyClass) });
ILGenerator il = dm.GetILGenerator();
il.Emit(OpCodes.Ldarg_0);
il.EmitCall(OpCodes.Callvirt, methInf, null);
需要什么才能使输出参数起作用?
【问题讨论】:
-
澄清一下,您的问题是关于如何向具有
out参数的方法发出调用? -
由于参数已经是 ref 类型,您应该只需要加载参数(是一个引用) - 无论是 arg0或 arg1 取决于实例与静态
-
ref和out实际上与 CLR 相同。它们的区别仅在于编译器如何强制代码在方法返回之前设置out。
标签: c# cil emit out-parameters ilgenerator