I want to add a new method via Mono.Cecil which has an output parameter, like:

private static bool XXXXX(out Int32 a)

I tried the following codes to add this parameter

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

And I compare the IL codes between the one I added and the one complier generates. they are different.

Mine added by Cecil:

.method private hidebysig static bool XXXXX([out] int32 a) cil managed

Compiler generates:

.method private hidebysig static bool XXXXX([out] int32& a) cil managed

Please anyone knows how to make my Cecil added method the same as the complier generates?

有帮助吗?

解决方案

I think the type has to be by reference: int32& (or ref int in C# syntax)

ByReferenceType typeInt32ByRef = new ByReferenceType(typeInt32);
method.Parameters.Add(
    new ParameterDefinition(typeInt32ByRef) { Name = "a", IsOut = true });
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top