Skip to content

Commit

Permalink
fix #38
Browse files Browse the repository at this point in the history
  • Loading branch information
zdyu committed Jan 4, 2024
1 parent 0cf175a commit 4185c0d
Show file tree
Hide file tree
Showing 7 changed files with 372 additions and 31 deletions.
6 changes: 3 additions & 3 deletions src/Autofac.Annotation.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -28,10 +28,10 @@
<RootNamespace>Autofac.Annotation</RootNamespace>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<PackageRequireLicenseAcceptance>true</PackageRequireLicenseAcceptance>
<Version>4.4.3</Version>
<AssemblyVersion>4.4.3.0</AssemblyVersion>
<Version>4.4.4</Version>
<AssemblyVersion>4.4.4.0</AssemblyVersion>
<Authors>zdyu</Authors>
<PackageVersion>4.4.3</PackageVersion>
<PackageVersion>4.4.4</PackageVersion>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
</PropertyGroup>

Expand Down
2 changes: 1 addition & 1 deletion src/AutofacAnnotationModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ private bool NeedWarpForPointcut(ComponentModel component, PointCutConfiguration
if (aspectClass.Pointcut.IsVaild(aspectClass, method, pointCutClassInjectAnotation,
out var pointCutMethodInjectAnotation))
{
if (component.isDynamicGeneric || isgeneric)
if (component.isDynamicGeneric || isgeneric || method.IsGenericMethod)
{
var uniqKey = method.GetMethodInfoUniqueName();
if (pointCutCfg.DynamicPointcutTargetInfoList.ContainsKey(uniqKey))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ private void AddCacheInter(ComponentModel aspectClass, bool isGeneric = false)

aspectAttributeInfo.AdviceMethod = aspectAttributeInfo.AdviceMethod.OrderBy(r => r.OrderIndex).ToList();

if (aspectClass.isDynamicGeneric || isGeneric)
if (aspectClass.isDynamicGeneric || isGeneric || method.IsGenericMethod)
{
DynamicCacheList.TryAdd(method.GetMethodInfoUniqueName(), aspectAttributeInfo);
continue;
Expand Down
12 changes: 6 additions & 6 deletions src/Intercepter/AspectIntercepter/AdviceIntercept.cs
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ internal void InterceptInternal(IInvocation invocation)

if (!_cache.CacheList.TryGetValue(invocation.Method, out var attribute))
{
if (!_cache.CacheList.TryGetValue(invocation.MethodInvocationTarget, out var attributeInherited))
if (invocation.Method == invocation.MethodInvocationTarget ||
!_cache.CacheList.TryGetValue(invocation.MethodInvocationTarget, out var attributeInherited))
{
//动态泛型类
if (!invocation.MethodInvocationTarget.DeclaringType.GetTypeInfo().IsGenericType ||
!_cache.DynamicCacheList.TryGetValue(
if (!_cache.DynamicCacheList.TryGetValue(
invocation.MethodInvocationTarget.GetMethodInfoUniqueName(),
out var AttributesDynamic))
{
Expand Down Expand Up @@ -106,11 +106,11 @@ internal async ValueTask InterceptInternalAsync(IAsyncInvocation invocation)

if (!_cache.CacheList.TryGetValue(invocation.Method, out var attribute))
{
if (!_cache.CacheList.TryGetValue(invocation.TargetMethod, out var attributeInherited))
if (invocation.Method == invocation.TargetMethod ||
!_cache.CacheList.TryGetValue(invocation.TargetMethod, out var attributeInherited))
{
//动态泛型类
if (!invocation.TargetMethod.DeclaringType.GetTypeInfo().IsGenericType ||
!_cache.DynamicCacheList.TryGetValue(invocation.TargetMethod.GetMethodInfoUniqueName(),
if (!_cache.DynamicCacheList.TryGetValue(invocation.TargetMethod.GetMethodInfoUniqueName(),
out var AttributesDynamic))
{
await invocation.ProceedAsync();
Expand Down
6 changes: 2 additions & 4 deletions src/Intercepter/AspectIntercepter/PointcutIntercept.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,7 @@ protected override void Intercept(IInvocation invocation)
{
if (!_configuration.CacheList.TryGetValue(new ObjectKey(invocation.TargetType, invocation.MethodInvocationTarget), out var pointCutInherited))
{
if (!invocation.MethodInvocationTarget.DeclaringType.GetTypeInfo().IsGenericType ||
!_configuration.DynamicCacheList.TryGetValue(invocation.MethodInvocationTarget.GetMethodInfoUniqueName(),
if (!_configuration.DynamicCacheList.TryGetValue(invocation.MethodInvocationTarget.GetMethodInfoUniqueName(),
out var pointCutDynamic))
{
//该方法不需要拦截
Expand Down Expand Up @@ -96,8 +95,7 @@ protected override async ValueTask InterceptAsync(IAsyncInvocation invocation)
{
if (!_configuration.CacheList.TryGetValue(new ObjectKey(invocation.TargetType, invocation.TargetMethod), out var pointCutInherited))
{
if (!invocation.TargetMethod.DeclaringType.GetTypeInfo().IsGenericType ||
!_configuration.DynamicCacheList.TryGetValue(invocation.TargetMethod.GetMethodInfoUniqueName(),
if (!_configuration.DynamicCacheList.TryGetValue(invocation.TargetMethod.GetMethodInfoUniqueName(),
out var pointCutDynamic))
{
//该方法不需要拦截
Expand Down
33 changes: 17 additions & 16 deletions src/Util/ReflectionExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -132,22 +132,23 @@ public static bool IsAssignableFromInterfaceMethod(this MethodInfo mi, MethodInf
/// <returns></returns>
public static string GetMethodInfoUniqueName(this MethodInfo mi)
{
var signatureString = string.Join(",", mi.GetParameters().Select(p => p.ParameterType.Name).ToArray());
var returnTypeName = mi.ReturnType.Name;

if (mi.IsGenericMethod)
{
var typeParamsString = string.Join(",",
mi.GetGenericArguments().Select(g => g.AssemblyQualifiedName).ToArray());


// returns a string like this: "Assembly.YourSolution.YourProject.YourClass:YourMethod(Param1TypeName,...,ParamNTypeName):ReturnTypeName
return
$"{mi.DeclaringType.Namespace+"," + mi.DeclaringType.Name}:{mi.Name}<{typeParamsString}>({signatureString}):{returnTypeName}";
}

return
$"{mi.DeclaringType.Namespace +","+ mi.DeclaringType.Name}:{mi.Name}({signatureString}):{returnTypeName}";
return mi.Module.Name + "-" + mi.MetadataToken;
// var signatureString = string.Join(",", mi.GetParameters().Select(p => p.ParameterType.Name).ToArray());
// var returnTypeName = mi.ReturnType.Name;
//
// if (mi.IsGenericMethod)
// {
// var typeParamsString = string.Join(",",
// mi.GetGenericArguments().Select(g => g.AssemblyQualifiedName).ToArray());
//
//
// // returns a string like this: "Assembly.YourSolution.YourProject.YourClass:YourMethod(Param1TypeName,...,ParamNTypeName):ReturnTypeName
// return
// $"{mi.DeclaringType.Namespace+"," + mi.DeclaringType.Name}:{mi.Name}<{typeParamsString}>({signatureString}):{returnTypeName}";
// }
//
// return
// $"{mi.DeclaringType.Namespace +","+ mi.DeclaringType.Name}:{mi.Name}({signatureString}):{returnTypeName}";
}

/// <summary>
Expand Down
Loading

0 comments on commit 4185c0d

Please sign in to comment.