Skip to content

Problem with async methods in generic class fixed. #78

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion Fody/Cauldron.Interception.Cecilator/Extension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -767,4 +767,32 @@ private object ValueType_Method_With_Single_Return_Object_()
return 3434;
}
}

[TestClass]
public class Method_Interceptor_Code_Validation_Generic_Tests<T>
{
[TestMethodInterceptor]
public async Task Async_Method()
{
await Task.Run(() => default(T));
}

[TestMethodInterceptor]
public async Task<T> Generic_Async_Method()
{
return await Task.Run(() => default(T));
}

[TestMethodInterceptor]
public async Task<T> Generic_Async_Method_With_Parameters(int a, string b)
{
return await Task.Run(() => default(T));
}

[TestMethodInterceptor]
public async Task<T> Generic_Async_Method_With_Generic_Parameters<TParam1, TParam2>(TParam1 p1, int a, TParam2 p2, string b)
{
return await Task.Run(() => default(T));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,4 +93,32 @@ public int WithSyncRoot()
return 44;
}
}

[TestClass]
public class SimpleMethod_Interceptor_Code_Validation_Generic_Tests<T>
{
[SimpleInterceptor]
public async Task Async_Method()
{
await Task.Run(() => default(T));
}

[SimpleInterceptor]
public async Task<T> Generic_Async_Method()
{
return await Task.Run(() => default(T));
}

[SimpleInterceptor]
public async Task<T> Generic_Async_Method_With_Parameters(int a, string b)
{
return await Task.Run(() => default(T));
}

[SimpleInterceptor]
public async Task<T> Generic_Async_Method_With_Generic_Parameters<TParam1, TParam2>(TParam1 p1, int a, TParam2 p2, string b)
{
return await Task.Run(() => default(T));
}
}
}