@@ -37,7 +37,10 @@ public InjectionDefinition(MethodDefinition injectTarget,
3737 int [ ] localVarIDs = null ,
3838 params FieldDefinition [ ] memberReferences )
3939 {
40- ParameterCount = VerifyInjectionDefinition ( injectMethod , injectTarget , flags , localVarIDs ,
40+ ParameterCount = VerifyInjectionDefinition ( injectMethod ,
41+ injectTarget ,
42+ flags ,
43+ localVarIDs ,
4144 memberReferences ) ;
4245 InjectMethod = injectMethod ;
4346 InjectTarget = injectTarget ;
@@ -140,19 +143,19 @@ internal static InjectionDefinition FindInjectionDefinition(TypeDefinition type,
140143 int parameterCount = 0 ;
141144
142145 MethodDefinition injection =
143- type . Methods . FirstOrDefault ( m =>
144- {
145- try
146- {
147- parameterCount = VerifyInjectionDefinition ( m , target , flags , localVarIDs , memberReferences ) ;
148- }
149- catch ( InjectionDefinitionException e )
146+ type . Methods . FirstOrDefault ( m =>
150147 {
151- Logger . LogLine ( LogMask . GetInjectionMethod , e . Message ) ;
152- return false ;
153- }
154- return true ;
155- } ) ;
148+ try
149+ {
150+ parameterCount = VerifyInjectionDefinition ( m , target , flags , localVarIDs , memberReferences ) ;
151+ }
152+ catch ( InjectionDefinitionException e )
153+ {
154+ Logger . LogLine ( LogMask . GetInjectionMethod , e . Message ) ;
155+ return false ;
156+ }
157+ return true ;
158+ } ) ;
156159
157160 if ( injection == null )
158161 {
@@ -178,8 +181,10 @@ private static void Assert(bool val, string message)
178181 throw new InjectionDefinitionException ( message ) ;
179182 }
180183
181- private static int VerifyInjectionDefinition ( MethodDefinition injectMethod , MethodDefinition injectTarget ,
182- InjectFlags flags , int [ ] localVarIDs = null ,
184+ private static int VerifyInjectionDefinition ( MethodDefinition injectMethod ,
185+ MethodDefinition injectTarget ,
186+ InjectFlags flags ,
187+ int [ ] localVarIDs = null ,
183188 params FieldDefinition [ ] memberReferences )
184189 {
185190 Assert (
@@ -201,22 +206,25 @@ private static int VerifyInjectionDefinition(MethodDefinition injectMethod, Meth
201206 ! hFlags . PassFields || memberReferences != null ,
202207 $ "Supposed to pass member fields, but { nameof ( memberReferences ) } is empty!") ;
203208
204- int prefixCount = Convert . ToInt32 ( hFlags . PassTag ) + Convert . ToInt32 ( hFlags . PassInvokingInstance )
205- + Convert . ToInt32 ( hFlags . ModifyReturn && ! isVoid ) ;
209+ int prefixCount = Convert . ToInt32 ( hFlags . PassTag ) +
210+ Convert . ToInt32 ( hFlags . PassInvokingInstance ) +
211+ Convert . ToInt32 ( hFlags . ModifyReturn && ! isVoid ) ;
206212 int localsCount = hFlags . PassLocals ? localVarIDs . Length : 0 ;
207213 int memberRefCount = hFlags . PassFields ? memberReferences . Length : 0 ;
208214 int paramCount = hFlags . PassParameters ? injectTarget . Parameters . Count : 0 ;
209215 int parameters = injectMethod . Parameters . Count - prefixCount - localsCount - memberRefCount ;
210216
211217 Assert (
212- hFlags . PassParameters && 0 < parameters && parameters <= injectTarget . Parameters . Count
213- || ! hFlags . PassParameters && parameters == 0 ,
218+ hFlags . PassParameters && 0 < parameters && parameters <= injectTarget . Parameters . Count ||
219+ ! hFlags . PassParameters && parameters == 0 ,
214220 $@ "The injection method has a wrong number of parameters! Check that the provided target method, local variables, member references and injection flags add up to the right number of parameters.
215221Needed parameters: Prefix: { prefixCount } , Locals: { localsCount } , Members: { memberRefCount } , Parameters: {
216- ( hFlags . PassParameters ? "between 1 and " + paramCount : "0" )
217- } , TOTAL: {
218- ( hFlags . PassParameters ? $ "between { prefixCount + localsCount + memberRefCount + 1 } and " : "" )
219- } { prefixCount + localsCount + memberRefCount + paramCount } .
222+ ( hFlags . PassParameters ? "between 1 and " + paramCount : "0" )
223+ } , TOTAL: {
224+ ( hFlags . PassParameters
225+ ? $ "between { prefixCount + localsCount + memberRefCount + 1 } and "
226+ : "" )
227+ } { prefixCount + localsCount + memberRefCount + paramCount } .
220228Injection has { injectMethod . Parameters . Count } parameters." ) ;
221229
222230 TypeComparer comparer = TypeComparer . Instance ;
@@ -247,11 +255,12 @@ private static int VerifyInjectionDefinition(MethodDefinition injectMethod, Meth
247255 injectMethod . ReturnType . FullName == "System.Boolean" ,
248256 "The injection method must return a boolean in order to alter the return value." ) ;
249257 Assert (
250- isVoid
251- || comparer . Equals (
258+ isVoid ||
259+ comparer . Equals (
252260 injectMethod
253- . Parameters [ Convert . ToInt32 ( hFlags . PassTag ) + Convert . ToInt32 ( hFlags . PassInvokingInstance ) ]
254- . ParameterType ,
261+ . Parameters [ Convert . ToInt32 ( hFlags . PassTag ) +
262+ Convert . ToInt32 ( hFlags . PassInvokingInstance ) ]
263+ . ParameterType ,
255264 new ByReferenceType ( injectTarget . ReturnType ) ) ,
256265 "Supposed to modify the return value, but the provided return type does not match with the return type of the target method! Also make sure the type is passed by reference (out/ref)." ) ;
257266 }
@@ -267,11 +276,15 @@ private static int VerifyInjectionDefinition(MethodDefinition injectMethod, Meth
267276 "Supposed to receive local references, but the provided local variable index/indices do not exist in the target method!" ) ;
268277 Assert (
269278 injectMethod . Parameters . Slice ( prefixCount , localsCount )
270- . Select ( ( p , i ) => new { param = p , index = localVarIDs [ i ] } )
279+ . Select ( ( p , i ) => new
280+ {
281+ param = p ,
282+ index = localVarIDs [ i ]
283+ } )
271284 . All (
272285 t =>
273- t . param . ParameterType . IsByReference
274- && comparer . Equals (
286+ t . param . ParameterType . IsByReference &&
287+ comparer . Equals (
275288 t . param . ParameterType ,
276289 new ByReferenceType ( injectTarget . Body . Variables [ t . index ] . VariableType ) ) ) ,
277290 "Supposed to receive local references, but the types between injection method and target method mismatch. Also make sure they are passed by reference (ref/out)." ) ;
@@ -286,13 +299,13 @@ private static int VerifyInjectionDefinition(MethodDefinition injectMethod, Meth
286299 Assert (
287300 memberReferences . All (
288301 m =>
289- m . DeclaringType . FullName == injectTarget . DeclaringType . FullName
290- && m . DeclaringType . BaseType . FullName == injectTarget . DeclaringType . BaseType . FullName ) ,
302+ m . DeclaringType . FullName == injectTarget . DeclaringType . FullName &&
303+ m . DeclaringType . BaseType . FullName == injectTarget . DeclaringType . BaseType . FullName ) ,
291304 $ "The provided member fields do not belong to { injectTarget . DeclaringType } ") ;
292305
293306 IEnumerable < TypeReference > paramRefs =
294- injectMethod . Parameters . Slice ( prefixCount + localsCount , memberRefCount )
295- . Select ( p => p . ParameterType ) ;
307+ injectMethod . Parameters . Slice ( prefixCount + localsCount , memberRefCount )
308+ . Select ( p => p . ParameterType ) ;
296309 IEnumerable < TypeReference > typeReferences = paramRefs as TypeReference [ ] ?? paramRefs . ToArray ( ) ;
297310
298311 Assert (
@@ -313,15 +326,16 @@ private static int VerifyInjectionDefinition(MethodDefinition injectMethod, Meth
313326 "The injection and target methods have mismatching specification of generic parameters!" ) ;
314327
315328 Assert (
316- ! injectMethod . HasGenericParameters
317- || injectMethod . GenericParameters . Count <= injectTarget . GenericParameters . Count +
329+ ! injectMethod . HasGenericParameters ||
330+ injectMethod . GenericParameters . Count <=
331+ injectTarget . GenericParameters . Count +
318332 injectTarget . DeclaringType . GenericParameters . Count ,
319333 "The injection and target methods have a mismatching number of generic parameters! The injection method must have less or the same number of generic parameters as the target!" ) ;
320334
321335 Assert (
322- ! hFlags . PassParametersByRef
323- || injectMethod . Parameters . Skip ( prefixCount + localsCount + memberRefCount )
324- . All ( p => p . ParameterType . IsByReference ) ,
336+ ! hFlags . PassParametersByRef ||
337+ injectMethod . Parameters . Skip ( prefixCount + localsCount + memberRefCount )
338+ . All ( p => p . ParameterType . IsByReference ) ,
325339 "Supposed to pass target method parameters by reference, but the provided parameters in the injection method are not of a reference type (ref)." ) ;
326340
327341 Assert (
@@ -369,7 +383,8 @@ public void Inject(int startCode = 0, object token = null, InjectDirection direc
369383 /// parameter to the injection method.
370384 /// </param>
371385 /// <param name="direction">The direction in which to insert the call: either above the start code or below it.</param>
372- public void Inject ( Instruction startCode , object token = null ,
386+ public void Inject ( Instruction startCode ,
387+ object token = null ,
373388 InjectDirection direction = InjectDirection . Before )
374389 {
375390 InjectValues flags = Flags . ToValues ( ) ;
@@ -397,10 +412,11 @@ public void Inject(Instruction startCode, object token = null,
397412 MethodReference hookRef = InjectTarget . Module . Import ( InjectMethod ) ;
398413
399414 // If the hook is generic but not instantiated fully, attempt to fill in the generic arguments with the ones specified in the target method/class
400- if ( hookRef . HasGenericParameters && ( ! hookRef . IsGenericInstance ||
401- hookRef . IsGenericInstance &&
402- ( ( GenericInstanceMethod ) hookRef ) . GenericArguments . Count <
403- hookRef . GenericParameters . Count ) )
415+ if ( hookRef . HasGenericParameters &&
416+ ( ! hookRef . IsGenericInstance ||
417+ hookRef . IsGenericInstance &&
418+ ( ( GenericInstanceMethod ) hookRef ) . GenericArguments . Count <
419+ hookRef . GenericParameters . Count ) )
404420 {
405421 GenericInstanceMethod genericInjectMethod = new GenericInstanceMethod ( hookRef ) ;
406422 foreach ( GenericParameter genericParameter in InjectMethod . GenericParameters )
0 commit comments