Skip to content

Commit fdf640c

Browse files
committed
use default implementation when creating proxy type
1 parent a7d1dea commit fdf640c

File tree

2 files changed

+28
-0
lines changed

2 files changed

+28
-0
lines changed

src/AspectCore.Core/Utils/ProxyGeneratorUtils.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -375,6 +375,10 @@ internal static void DefineInterfaceImplMethods(Type[] interfaceTypes, TypeBuild
375375

376376
internal static MethodBuilder DefineInterfaceImplMethod(MethodInfo method, TypeBuilder implTypeBuilder)
377377
{
378+
// method is not abstract means it is a default implementation on the interface, so don't need to define method on the proxy type.
379+
if (method.IsAbstract == false)
380+
return null;
381+
378382
var methodBuilder = implTypeBuilder.DefineMethod(method.Name, InterfaceMethodAttributes, method.CallingConvention, method.ReturnType, method.GetParameterTypes());
379383
var ilGen = methodBuilder.GetILGenerator();
380384
if (method.ReturnType != typeof(void))
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
using AspectCore.DynamicProxy;
2+
using AspectCore.Tests.DynamicProxy;
3+
using Xunit;
4+
5+
namespace AspectCore.Tests.Issues.DynamicProxy;
6+
7+
// https://github.com/dotnetcore/AspectCore-Framework/issues/223
8+
public class InterfaceDefaultMethodsShouldBeUsedTests : DynamicProxyTestBase
9+
{
10+
public interface IService
11+
{
12+
int Get() => 1;
13+
}
14+
15+
public class Service : IService{}
16+
17+
[Fact]
18+
public void CreateInterfaceProxy_WithoutImplementationType_Test()
19+
{
20+
var service = ProxyGenerator.CreateInterfaceProxy<IService>();
21+
var result = service.Get();
22+
Assert.Equal(1, result);
23+
}
24+
}

0 commit comments

Comments
 (0)