Skip to content
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

use interface's default implementation when creating proxy type #332

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
4 changes: 4 additions & 0 deletions src/AspectCore.Core/Utils/ProxyGeneratorUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -375,6 +375,10 @@ internal static void DefineInterfaceImplMethods(Type[] interfaceTypes, TypeBuild

internal static MethodBuilder DefineInterfaceImplMethod(MethodInfo method, TypeBuilder implTypeBuilder)
{
// method is not abstract means it is a default implementation on the interface, so don't need to define method on the proxy type.
if (method.IsAbstract == false)
return null;

var methodBuilder = implTypeBuilder.DefineMethod(method.Name, InterfaceMethodAttributes, method.CallingConvention, method.ReturnType, method.GetParameterTypes());
var ilGen = methodBuilder.GetILGenerator();
if (method.ReturnType != typeof(void))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using AspectCore.DynamicProxy;
using AspectCore.Tests.DynamicProxy;
using Xunit;

namespace AspectCore.Tests.Issues.DynamicProxy;

// https://github.com/dotnetcore/AspectCore-Framework/issues/223
public class InterfaceDefaultMethodsShouldBeUsedTests : DynamicProxyTestBase
{
public interface IService
{
int Get() => 1;
}

public class Service : IService{}

[Fact]
public void CreateInterfaceProxy_WithoutImplementationType_Test()
{
var service = ProxyGenerator.CreateInterfaceProxy<IService>();
var result = service.Get();
Assert.Equal(1, result);
}
}
Loading