From 9b58343d3b136ba755f562be87dab0f4352909fa Mon Sep 17 00:00:00 2001 From: Scott Xu Date: Sat, 28 Oct 2017 22:25:16 +0800 Subject: [PATCH] Fixed https://github.com/ninject/Ninject.Extensions.Interception/issues/26 Fixed: unexpected interceptor is chosen when attribute is applied to any of the derived classes. --- CHANGELOG.md | 5 +++++ .../Fakes/Derived.cs | 6 ++++++ .../MethodInterceptionContext.cs | 21 +++++++++++++++++++ .../Advice/Advice.cs | 7 ++++++- 4 files changed, 38 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a5e037a..1befa1a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/Ninject.Extensions.Interception.Test/Fakes/Derived.cs b/src/Ninject.Extensions.Interception.Test/Fakes/Derived.cs index d8cafd0..81619a0 100644 --- a/src/Ninject.Extensions.Interception.Test/Fakes/Derived.cs +++ b/src/Ninject.Extensions.Interception.Test/Fakes/Derived.cs @@ -9,4 +9,10 @@ public void DoDerived() { } } + + public class Derived2 : Base, IDerived + { + public void DoDerived() + { } + } } \ No newline at end of file diff --git a/src/Ninject.Extensions.Interception.Test/MethodInterceptionContext.cs b/src/Ninject.Extensions.Interception.Test/MethodInterceptionContext.cs index 7cb2ccc..b81459f 100644 --- a/src/Ninject.Extensions.Interception.Test/MethodInterceptionContext.cs +++ b/src/Ninject.Extensions.Interception.Test/MethodInterceptionContext.cs @@ -1,5 +1,6 @@ namespace Ninject.Extensions.Interception { + using System.Linq; using FluentAssertions; using Ninject.Extensions.Interception.Fakes; @@ -177,6 +178,26 @@ public void MethodsFromDerivedClassesCanBeIntercepted() } } + [Fact] + public void MethodsFromDerivedClassesWithAttributeCanBeIntercepted() + { + using (var kernel = CreateDefaultInterceptionKernel()) + { + CountInterceptor.Reset(); + + kernel.Bind().To(); + kernel.Bind().To(); + + var objs = kernel.GetAll().ToList(); + + objs[0].DoDerived(); + CountInterceptor.Count.Should().Be(1); + + objs[1].DoDerived(); + CountInterceptor.Count.Should().Be(1); + } + } + [Fact] public void ClassesWithNoDefaultConstructorCanBeIntercepted() { diff --git a/src/Ninject.Extensions.Interception/Advice/Advice.cs b/src/Ninject.Extensions.Interception/Advice/Advice.cs index f990bbf..05a7ff9 100644 --- a/src/Ninject.Extensions.Interception/Advice/Advice.cs +++ b/src/Ninject.Extensions.Interception/Advice/Advice.cs @@ -164,6 +164,11 @@ private bool MatchesMethodPredicate(IProxyRequest request) /// true if XXXX, false otherwise. private bool MatchesMethod(IProxyRequest request) { + if (!this.method.DeclaringType.IsAssignableFrom(request.Target.GetType())) + { + return false; + } + if (request.Method.GetMethodHandle().Equals(this.MethodHandle)) { return true; @@ -186,6 +191,6 @@ private bool MatchesMethod(IProxyRequest request) } return map.TargetMethods[index].GetMethodHandle() == this.method.GetMethodHandle(); - } + } } } \ No newline at end of file