Skip to content

Commit

Permalink
Fixed #2, fixed method injection of methods with generic parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
ghorsington committed Jun 12, 2016
1 parent be1f165 commit be41c02
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 5 deletions.
23 changes: 20 additions & 3 deletions Mono.Cecil.Inject/InjectionDefinition.cs
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,13 @@ Injection has {injectMethod.Parameters.Count
if (hFlags.PassParameters)
{
Assert(
injectMethod.HasGenericParameters == injectTarget.HasGenericParameters,
injectMethod.HasGenericParameters && injectTarget.Parameters.Any(p => p.ParameterType.IsGenericParameter),
"The injection and target methods have mismatching specification of generic parameters!");

Assert(
!injectMethod.HasGenericParameters
|| injectMethod.GenericParameters.Count <= injectTarget.GenericParameters.Count,
"The injection and target methods have a mismatching number of generic parameters! The injection method must have less or the same number of generic paramters as the target!");
|| injectMethod.GenericParameters.Count <= injectTarget.GenericParameters.Count + injectTarget.DeclaringType.GenericParameters.Count,
"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!");

Assert(
!hFlags.PassParametersByRef
Expand Down Expand Up @@ -293,6 +293,23 @@ public void Inject(Instruction startCode, int token = 0, InjectDirection directi

MethodReference hookRef = InjectTarget.Module.Import(InjectMethod);

// 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
if (hookRef.HasGenericParameters && (!hookRef.IsGenericInstance || hookRef.IsGenericInstance && ((GenericInstanceMethod)hookRef).GenericArguments.Count < hookRef.GenericParameters.Count))
{
GenericInstanceMethod genericInjectMethod = new GenericInstanceMethod(hookRef);
foreach (GenericParameter genericParameter in InjectMethod.GenericParameters)
{
List<GenericParameter> @params = new List<GenericParameter>();
@params.AddRange(InjectTarget.GenericParameters);
@params.AddRange(InjectTarget.DeclaringType.GenericParameters);
GenericParameter param = @params.FirstOrDefault(p => p.Name == genericParameter.Name);
if (param == null)
throw new Exception("Could not find a suitable type to bind to the generic injection method. Try to manually instantiate the generic injection method before injecting.");
genericInjectMethod.GenericArguments.Add(param);
}
hookRef = genericInjectMethod;
}

MethodBody targetBody = InjectTarget.Body;
ILProcessor il = targetBody.GetILProcessor();
int startIndex = targetBody.Instructions.IndexOf(startCode);
Expand Down
4 changes: 2 additions & 2 deletions Mono.Cecil.Inject/Properties/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@
// by using the '*' as shown below:
// [assembly: AssemblyVersion("1.0.*")]

[assembly: AssemblyVersion("1.2.1.0")]
[assembly: AssemblyFileVersion("1.2.1.0")]
[assembly: AssemblyVersion("1.2.2.0")]
[assembly: AssemblyFileVersion("1.2.2.0")]

0 comments on commit be41c02

Please sign in to comment.