private void InstantiateCollection(TypeDefinition typeDef, MethodDefinition[] constructors, PropertyDefinition propDef) { var constructor = constructors.First(); if (constructors.Length > 1) { constructor = constructors.SingleOrDefault(s => !s.HasParameters && !s.IsStatic); if (constructor == null) { this.Log.Error("Type " + typeDef.FullName + " does not have a parameterless constructor for instantiating collections in"); } } var insertIdx = constructor.Body.Instructions.Count - 1; constructor.Body.Instructions.Insert(insertIdx++, Instruction.Create(OpCodes.Nop)); constructor.Body.Instructions.Insert(insertIdx++, Instruction.Create(OpCodes.Ldarg_0)); constructor.Body.Instructions.Insert( insertIdx++, Instruction.Create( OpCodes.Newobj, MakeGeneric( typeDef.Module.Import( typeDef.Module.Import(typeof(List<>)) .MakeGenericInstanceType(propDef.PropertyType) .Resolve() .GetConstructors() .First(c => !c.HasParameters)), ((GenericInstanceType)propDef.PropertyType).GenericArguments.First()))); constructor.Body.Instructions.Insert(insertIdx, Instruction.Create(OpCodes.Call, propDef.SetMethod)); }