Skip to content

Commit 9b58343

Browse files
committed
Fixed #26
Fixed: unexpected interceptor is chosen when attribute is applied to any of the derived classes.
1 parent a7d48db commit 9b58343

File tree

4 files changed

+38
-1
lines changed

4 files changed

+38
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
44
The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
55
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
66

7+
## [Unreleased]
8+
9+
### Fixed
10+
- Unexpected interceptor is chosen when attribute is applied to any of the derived classes. [#26](https://github.com/ninject/Ninject.Extensions.Interception/issues/26)
11+
712
## [3.3.2] - 2017-10-22
813

914
### Added

src/Ninject.Extensions.Interception.Test/Fakes/Derived.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,10 @@ public void DoDerived()
99
{
1010
}
1111
}
12+
13+
public class Derived2 : Base, IDerived
14+
{
15+
public void DoDerived()
16+
{ }
17+
}
1218
}

src/Ninject.Extensions.Interception.Test/MethodInterceptionContext.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
namespace Ninject.Extensions.Interception
22
{
3+
using System.Linq;
34
using FluentAssertions;
45

56
using Ninject.Extensions.Interception.Fakes;
@@ -177,6 +178,26 @@ public void MethodsFromDerivedClassesCanBeIntercepted()
177178
}
178179
}
179180

181+
[Fact]
182+
public void MethodsFromDerivedClassesWithAttributeCanBeIntercepted()
183+
{
184+
using (var kernel = CreateDefaultInterceptionKernel())
185+
{
186+
CountInterceptor.Reset();
187+
188+
kernel.Bind<IDerived>().To<Derived>();
189+
kernel.Bind<IDerived>().To<Derived2>();
190+
191+
var objs = kernel.GetAll<IDerived>().ToList();
192+
193+
objs[0].DoDerived();
194+
CountInterceptor.Count.Should().Be(1);
195+
196+
objs[1].DoDerived();
197+
CountInterceptor.Count.Should().Be(1);
198+
}
199+
}
200+
180201
[Fact]
181202
public void ClassesWithNoDefaultConstructorCanBeIntercepted()
182203
{

src/Ninject.Extensions.Interception/Advice/Advice.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,6 +164,11 @@ private bool MatchesMethodPredicate(IProxyRequest request)
164164
/// <returns><c>true</c> if XXXX, <c>false</c> otherwise.</returns>
165165
private bool MatchesMethod(IProxyRequest request)
166166
{
167+
if (!this.method.DeclaringType.IsAssignableFrom(request.Target.GetType()))
168+
{
169+
return false;
170+
}
171+
167172
if (request.Method.GetMethodHandle().Equals(this.MethodHandle))
168173
{
169174
return true;
@@ -186,6 +191,6 @@ private bool MatchesMethod(IProxyRequest request)
186191
}
187192

188193
return map.TargetMethods[index].GetMethodHandle() == this.method.GetMethodHandle();
189-
}
194+
}
190195
}
191196
}

0 commit comments

Comments
 (0)