Mono.Cecil如何定义输出参数

Mr.*_*oor 4 .net il mono.cecil

我想通过Mono.Cecil添加一个具有输出参数的新方法,例如:

private static bool XXXXX(out Int32 a)
Run Code Online (Sandbox Code Playgroud)

我尝试了以下代码来添加此参数

TypeReference typeInt32 = targetAssembly.MainModule.TypeSystem.Int32.Resolve();
typeInt32 = targetAssembly.MainModule.Import(typeInt32);
method.Parameters.Add(new ParameterDefinition(typeInt32) { Name = "a", IsOut = true });
Run Code Online (Sandbox Code Playgroud)

然后,我在添加的IL和编译器生成的IL代码之间进行比较。它们不一样。

Cecil添加的矿井:

.method private hidebysig static bool XXXXX([out] int32 a) cil managed
Run Code Online (Sandbox Code Playgroud)

编译器生成:

.method private hidebysig static bool XXXXX([out] int32& a) cil managed
Run Code Online (Sandbox Code Playgroud)

请谁知道如何使我的Cecil添加方法与编译器生成的方法相同?

svi*_*ick 5

我认为类型必须通过引用:(int32&ref int使用C#语法)

ByReferenceType typeInt32ByRef = new ByReferenceType(typeInt32);
method.Parameters.Add(
    new ParameterDefinition(typeInt32ByRef) { Name = "a", IsOut = true });
Run Code Online (Sandbox Code Playgroud)