如何使用out参数生成方法

问题描述 投票:0回答:1

使用System.Reflection,如何使用out参数生成方法?

我可以使用ref生成MakeByRefType但我找不到任何关于任何MakeOutType ...

typeBuilder.DefineMethod("myfunc", MethodAttributes.Public | MethodAttributes.Static, typeof(void), new Type[] { typeof(int).MakeByRefType() });
c# reflection system.reflection
1个回答
0
投票

那是因为没有out int类型。 Out是attribute,关键字out是一种语法糖。

您必须在参数定义中指定此参数属性:

var mb = typeBuilder.DefineMethod("myfunc", MethodAttributes.Public | MethodAttributes.Static, typeof(void), new Type[] { typeof(int).MakeByRefType() });
var paramBuilder = mb.DefineParameter(1, ParameterAttributes.Out, "a");
// or: paramBuilder.SetCustomAttribute(new CustomAttributeBuilder(typeof(OutAttribute).GetConstructor(new Type[0]), new object[0]));
© www.soinside.com 2019 - 2024. All rights reserved.