Skip to content

Commit 2082174

Browse files
q7164518liuhaoyang
authored andcommitted
支持C#8.0的接口默认方法 (#189)
* 支持C#8.0的接口默认方法 * 支持C#8.0接口默认方法, 解决默认方法不拦截的问题
1 parent 61948ea commit 2082174

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

core/src/AspectCore.Core/Utils/ProxyGeneratorUtils.cs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -462,7 +462,32 @@ private static MethodBuilder DefineMethod(MethodInfo method, string name, Method
462462
var implementationMethod = implType.GetTypeInfo().GetMethodBySignature(method);
463463
if (implementationMethod == null)
464464
{
465-
throw new MissingMethodException($"Type '{implType}' does not contain a method '{method}'.");
465+
var interfaces = implType.GetInterfaces();
466+
if (interfaces == null || interfaces.Length <= 0)
467+
{
468+
throw new MissingMethodException($"Type '{implType}' does not contain a method '{method}'.");
469+
}
470+
var @interface = interfaces.Where(f => f.GetCustomAttribute(typeof(AbstractInterceptorAttribute)) != null).ToArray();
471+
if (@interface.Length > 0)
472+
{
473+
foreach (var item in @interface)
474+
{
475+
implementationMethod = item.GetTypeInfo().GetMethodBySignature(method);
476+
if (implementationMethod != null) break;
477+
}
478+
}
479+
else
480+
{
481+
foreach (var item in interfaces)
482+
{
483+
implementationMethod = item.GetTypeInfo().GetMethodBySignature(method);
484+
if (implementationMethod != null) break;
485+
}
486+
}
487+
if (implementationMethod == null)
488+
{
489+
throw new MissingMethodException($"Type '{implType}' does not contain a method '{method}'.");
490+
}
466491
}
467492

468493
if (method.IsNonAspect())

0 commit comments

Comments
 (0)