Skip to content

Commit

Permalink
Fixed #26
Browse files Browse the repository at this point in the history
Fixed: unexpected interceptor is chosen when attribute is applied to any
of the derived classes.
  • Loading branch information
scott-xu committed Oct 28, 2017
1 parent a7d48db commit 9b58343
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 1 deletion.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Fixed
- Unexpected interceptor is chosen when attribute is applied to any of the derived classes. [#26](https://github.com/ninject/Ninject.Extensions.Interception/issues/26)

## [3.3.2] - 2017-10-22

### Added
Expand Down
6 changes: 6 additions & 0 deletions src/Ninject.Extensions.Interception.Test/Fakes/Derived.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,10 @@ public void DoDerived()
{
}
}

public class Derived2 : Base, IDerived
{
public void DoDerived()
{ }
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
namespace Ninject.Extensions.Interception
{
using System.Linq;
using FluentAssertions;

using Ninject.Extensions.Interception.Fakes;
Expand Down Expand Up @@ -177,6 +178,26 @@ public void MethodsFromDerivedClassesCanBeIntercepted()
}
}

[Fact]
public void MethodsFromDerivedClassesWithAttributeCanBeIntercepted()
{
using (var kernel = CreateDefaultInterceptionKernel())
{
CountInterceptor.Reset();

kernel.Bind<IDerived>().To<Derived>();
kernel.Bind<IDerived>().To<Derived2>();

var objs = kernel.GetAll<IDerived>().ToList();

objs[0].DoDerived();
CountInterceptor.Count.Should().Be(1);

objs[1].DoDerived();
CountInterceptor.Count.Should().Be(1);
}
}

[Fact]
public void ClassesWithNoDefaultConstructorCanBeIntercepted()
{
Expand Down
7 changes: 6 additions & 1 deletion src/Ninject.Extensions.Interception/Advice/Advice.cs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,11 @@ private bool MatchesMethodPredicate(IProxyRequest request)
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
private bool MatchesMethod(IProxyRequest request)
{
if (!this.method.DeclaringType.IsAssignableFrom(request.Target.GetType()))
{
return false;
}

if (request.Method.GetMethodHandle().Equals(this.MethodHandle))
{
return true;
Expand All @@ -186,6 +191,6 @@ private bool MatchesMethod(IProxyRequest request)
}

return map.TargetMethods[index].GetMethodHandle() == this.method.GetMethodHandle();
}
}
}
}

0 comments on commit 9b58343

Please sign in to comment.