@@ -104,7 +104,7 @@ private void ExecuteInjectDeepCopy()
104104
105105 var constructorResolved = constructor . Resolve ( ) ;
106106 constructorResolved . Body . SimplifyMacros ( ) ;
107- InsertCopyInstructions ( target , constructorResolved . Body , 2 ) ;
107+ InsertCopyInstructions ( target , constructorResolved . Body ) ;
108108 constructorResolved . CustomAttributes . Remove ( constructorResolved . SingleAttribute ( InjectDeepCopyAttribute ) ) ;
109109 }
110110 }
@@ -115,7 +115,6 @@ private void AddDeepConstructor(TypeDefinition type)
115115 constructor . Parameters . Add ( new ParameterDefinition ( type ) ) ;
116116
117117 var processor = constructor . Body . GetILProcessor ( ) ;
118- var offset = 2 ;
119118
120119 if ( type . BaseType . Resolve ( ) . MetadataToken == TypeSystem . ObjectDefinition . MetadataToken )
121120 {
@@ -127,24 +126,24 @@ private void AddDeepConstructor(TypeDefinition type)
127126 processor . Emit ( OpCodes . Ldarg_0 ) ;
128127 processor . Emit ( OpCodes . Ldarg_1 ) ;
129128 processor . Emit ( OpCodes . Call , baseConstructor ) ;
130- offset = 3 ;
131129 }
132130 else
133131 throw new CopyConstructorRequiredException ( type . BaseType ) ;
134132
135- InsertCopyInstructions ( type , constructor . Body , offset ) ;
133+ InsertCopyInstructions ( type , constructor . Body ) ;
136134
137135 processor . Emit ( OpCodes . Ret ) ;
138136 type . Methods . Add ( constructor ) ;
139137 }
140138
141- private void InsertCopyInstructions ( TypeDefinition type , MethodBody body , int offset )
139+ private void InsertCopyInstructions ( TypeDefinition type , MethodBody body )
142140 {
143141 _booleanVariable = null ;
144142 _indexVariable = null ;
145143 CurrentBody . Value = body ;
146144
147- var index = offset ;
145+ var baseConstructorCall = body . Instructions . Single ( i => i . OpCode == OpCodes . Call && i . Operand is MethodReference method && method . Name == ".ctor" ) ;
146+ var index = body . Instructions . IndexOf ( baseConstructorCall ) + 1 ;
148147 var properties = new List < string > ( ) ;
149148
150149 foreach ( var property in type . Properties )
0 commit comments