Trying to find the constructor manually can be difficult, but you still need to have the one you created earlier? Have you tried passing this? I will try to make an example ...
var assemblyName = new AssemblyName("tmp"); var assembly = AppDomain.CurrentDomain.DefineDynamicAssembly(assemblyName, AssemblyBuilderAccess.Run); var module = assembly.DefineDynamicModule("tmp"); var foo = module.DefineType("Foo"); var bar = module.DefineType("Bar"); var barOnFoo = foo.DefineField("bar", bar, FieldAttributes.Private); var fooOnBar = bar.DefineField("foo", foo, FieldAttributes.Private); var barCtor = bar.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, new Type[] { foo }); var il = barCtor.GetILGenerator(); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldarg_1); il.Emit(OpCodes.Stfld, fooOnBar); il.Emit(OpCodes.Ret); var fooCtor = foo.DefineConstructor(MethodAttributes.Public, CallingConventions.HasThis, Type.EmptyTypes); il = fooCtor.GetILGenerator(); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Ldarg_0); il.Emit(OpCodes.Newobj, barCtor); il.Emit(OpCodes.Stfld, barOnFoo); il.Emit(OpCodes.Ret);
I could add additional code to check the result, but it's easier to just look at obj in the debugger, and you can see the fields, etc.
For more complex cases - do not forget that you do not need to write the body of the method (IL) to use it ... you can first write all the signatures ( DefineMethod , DefineConstructor , etc.), and then write all the bodies after, allowing completely cyclic code.
source share