diff --git a/Fody/Cauldron.Interception.Cecilator/Extension.cs b/Fody/Cauldron.Interception.Cecilator/Extension.cs index d6a6fb82ab..7b96479d77 100644 --- a/Fody/Cauldron.Interception.Cecilator/Extension.cs +++ b/Fody/Cauldron.Interception.Cecilator/Extension.cs @@ -904,6 +904,12 @@ internal static FieldReference CreateFieldReference(this Field field, Method met { var declaringType = new GenericInstanceType(field.DeclaringType.typeDefinition); + if (method.methodReference.DeclaringType.IsGenericInstance && method.methodReference.DeclaringType is GenericInstanceType genericInstanceType) + { + foreach (var parameter in genericInstanceType.GenericArguments) + declaringType.GenericArguments.Add(parameter); + } + foreach (var parameter in method.methodReference.GenericParameters) declaringType.GenericArguments.Add(parameter); @@ -1442,7 +1448,7 @@ internal static MethodReference MakeHostInstanceGeneric(this MethodReference sel }; foreach (var parameter in self.Parameters) - reference.Parameters.Add(new ParameterDefinition(parameter.ParameterType)); + reference.Parameters.Add(new ParameterDefinition(parameter.ParameterType) { Name = parameter.Name }); foreach (var generic_parameter in self.GenericParameters) reference.GenericParameters.Add(new GenericParameter(generic_parameter.Name, reference)); diff --git a/UnitTests/Shared/MainTests/BasicInterceptors/Method_Interceptor_Code_Validation_Tests.cs b/UnitTests/Shared/MainTests/BasicInterceptors/Method_Interceptor_Code_Validation_Tests.cs index 5ebc5bc222..49ae442924 100644 --- a/UnitTests/Shared/MainTests/BasicInterceptors/Method_Interceptor_Code_Validation_Tests.cs +++ b/UnitTests/Shared/MainTests/BasicInterceptors/Method_Interceptor_Code_Validation_Tests.cs @@ -767,4 +767,32 @@ private object ValueType_Method_With_Single_Return_Object_() return 3434; } } + + [TestClass] + public class Method_Interceptor_Code_Validation_Generic_Tests + { + [TestMethodInterceptor] + public async Task Async_Method() + { + await Task.Run(() => default(T)); + } + + [TestMethodInterceptor] + public async Task Generic_Async_Method() + { + return await Task.Run(() => default(T)); + } + + [TestMethodInterceptor] + public async Task Generic_Async_Method_With_Parameters(int a, string b) + { + return await Task.Run(() => default(T)); + } + + [TestMethodInterceptor] + public async Task Generic_Async_Method_With_Generic_Parameters(TParam1 p1, int a, TParam2 p2, string b) + { + return await Task.Run(() => default(T)); + } + } } \ No newline at end of file diff --git a/UnitTests/Shared/MainTests/BasicInterceptors/SimpleMethod_Interceptor_Code_Validation_Tests.cs b/UnitTests/Shared/MainTests/BasicInterceptors/SimpleMethod_Interceptor_Code_Validation_Tests.cs index a712dd1e29..874cbf9a10 100644 --- a/UnitTests/Shared/MainTests/BasicInterceptors/SimpleMethod_Interceptor_Code_Validation_Tests.cs +++ b/UnitTests/Shared/MainTests/BasicInterceptors/SimpleMethod_Interceptor_Code_Validation_Tests.cs @@ -93,4 +93,32 @@ public int WithSyncRoot() return 44; } } + + [TestClass] + public class SimpleMethod_Interceptor_Code_Validation_Generic_Tests + { + [SimpleInterceptor] + public async Task Async_Method() + { + await Task.Run(() => default(T)); + } + + [SimpleInterceptor] + public async Task Generic_Async_Method() + { + return await Task.Run(() => default(T)); + } + + [SimpleInterceptor] + public async Task Generic_Async_Method_With_Parameters(int a, string b) + { + return await Task.Run(() => default(T)); + } + + [SimpleInterceptor] + public async Task Generic_Async_Method_With_Generic_Parameters(TParam1 p1, int a, TParam2 p2, string b) + { + return await Task.Run(() => default(T)); + } + } } \ No newline at end of file