You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
When I tried to append a stfld to a method body from a TypeDefinition with generic arguments, it got a different instruction with the expected code. Is there any wrong?
See following
stfld !0 Foo`1::list
and
stfld !0 class Foo`1<!0>::list
Sample
usingMono.Cecil;usingMono.Cecil.Cil;publicclassSnippetRunner{publicstaticvoidRun(stringpath){varmp=newModuleParameters{Architecture=TargetArchitecture.AMD64,Kind=ModuleKind.Dll,};using(varassembly=AssemblyDefinition.CreateAssembly(newAssemblyNameDefinition("Foo",Version.Parse("1.0.0.0")),Path.GetFileName(path),mp)){//Class : Foovarcls_Foo_0=newTypeDefinition("","Foo`1",TypeAttributes.AnsiClass|TypeAttributes.BeforeFieldInit|TypeAttributes.NotPublic,assembly.MainModule.TypeSystem.Object);assembly.MainModule.Types.Add(cls_Foo_0);vargp_T_1=newMono.Cecil.GenericParameter("T",cls_Foo_0);cls_Foo_0.GenericParameters.Add(gp_T_1);//Field: listvarfld_list_2=newFieldDefinition("list",FieldAttributes.Private,gp_T_1);cls_Foo_0.Fields.Add(fld_list_2);//Method : Barvarmd_Bar_3=newMethodDefinition("Bar",MethodAttributes.Private|MethodAttributes.HideBySig,assembly.MainModule.TypeSystem.Void);cls_Foo_0.Methods.Add(md_Bar_3);md_Bar_3.Body.InitLocals=true;varil_Bar_4=md_Bar_3.Body.GetILProcessor();//Parameters of 'void Bar(T a) => list = a;'varp_a_5=newParameterDefinition("a",ParameterAttributes.None,gp_T_1);md_Bar_3.Parameters.Add(p_a_5);il_Bar_4.Emit(OpCodes.Ldarg_0);il_Bar_4.Emit(OpCodes.Ldarg_1);il_Bar_4.Emit(OpCodes.Stfld,fld_list_2);il_Bar_4.Emit(OpCodes.Ret);//** Constructor: Foo() **varctor_Foo_6=newMethodDefinition(".ctor",MethodAttributes.Public|MethodAttributes.HideBySig|MethodAttributes.RTSpecialName|MethodAttributes.SpecialName,assembly.MainModule.TypeSystem.Void);cls_Foo_0.Methods.Add(ctor_Foo_6);varil_ctor_Foo_7=ctor_Foo_6.Body.GetILProcessor();il_ctor_Foo_7.Emit(OpCodes.Ldarg_0);il_ctor_Foo_7.Emit(OpCodes.Call,assembly.MainModule.ImportReference(TypeHelpers.DefaultCtorFor(cls_Foo_0.BaseType)));il_ctor_Foo_7.Emit(OpCodes.Ret);assembly.Write(path);}}}publicclassTypeHelpers{publicstaticMethodReferenceDefaultCtorFor(TypeReferencetype){varresolved=type.Resolve();if(resolved==null)returnnull;varctor=resolved.Methods.SingleOrDefault(m =>m.IsConstructor&&m.Parameters.Count==0&&!m.IsStatic);if(ctor==null)returnDefaultCtorFor(resolved.BaseType);returnnewMethodReference(".ctor",type.Module.TypeSystem.Void,type){HasThis=true};}}
Output:
.class private auto ansi beforefieldinit Foo`1<T> extends [mscorlib]System.Object
{
// Fields
.field private !T list
// Methods
.method private hidebysig instance void Bar (!T a) cil managed
{
// Method begins at RVA 0x2050
// Header size: 12
// Code size: 8 (0x8)
.maxstack 2
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: stfld !0 Foo`1::list
IL_0007: ret
} // end of method Foo`1::Bar
.method public hidebysig specialname rtspecialname instance void .ctor () cil managed {...}
} // end of class Foo`1
Expected:
.class private auto ansi beforefieldinit Foo`1<T> extends [mscorlib]System.Object
{
// Fields
.field private !T list
// Methods
.method private hidebysig instance void Bar (!T a) cil managed
{
// Method begins at RVA 0x2050
// Header size: 12
// Code size: 8 (0x8)
.maxstack 2
IL_0000: ldarg.0
IL_0001: ldarg.1
IL_0002: stfld !0 class Foo`1<!0>::list
IL_0007: ret
} // end of method Foo`1::Bar
.method public hidebysig specialname rtspecialname instance void .ctor () cil managed {...}
} // end of class Foo`1
reacted with thumbs up emoji reacted with thumbs down emoji reacted with laugh emoji reacted with hooray emoji reacted with confused emoji reacted with heart emoji reacted with rocket emoji reacted with eyes emoji
-
Background
When I tried to append a
stfld
to a method body from a TypeDefinition with generic arguments, it got a different instruction with the expected code. Is there any wrong?See following
and
Sample
Output:
Expected:
Beta Was this translation helpful? Give feedback.
All reactions