我有多个接口实现,我想为每个接口应用不同的拦截器,例如:
public interface IFoo { int Run(); }
public class Foo1 : IFoo
{
public int Run() { return 1; }
}
public class Foo2 : IFoo
{
public int Run() { return 2; }
}
拦截器示例:
public class MultiplyBy10 : IInterceptor
{
public void Intercept(IInvocation invocation)
{
invocation.Proceed();
invocation.ReturnValue = (int)invocation.ReturnValue * 10;
}
}
public class MultiplyBy100: IInterceptor
{
public void Intercept(IInvocation invocation)
{
invocation.Proceed();
invocation.ReturnValue = (int)invocation.ReturnValue * 100;
}
}
捆绑:
var kernel = new StandardKernel();
kernel.Bind<IFoo>().To<Foo1>().Named("1");
kernel.Bind<IFoo>().To<Foo2>().Named("2");
kernel.Intercept(ctx => ctx.Plan.Type == typeof(Foo1)).With<MultiplyBy10>();
kernel.Intercept(ctx => ctx.Plan.Type == typeof(Foo2)).With<MultiplyBy100>();
我希望 Foo1 乘以 10,Foo2 乘以 100。但相反,两者都乘以 10,即:
var foo1 = kernel.Get<IFoo>(ctx => ctx.Name == "1");
var foo2 = kernel.Get<IFoo>(ctx => ctx.Name == "2");
Assert.That(foo1.Run(), Is.EqualTo(10));
// fails: returns 20
Assert.That(foo2.Run(), Is.EqualTo(200));
这是一个错误还是我做错了什么?
这是一个
错误。它现在固定在 master: https://github.com/ninject/ninject.extensions.interception/commit/fb5ca07c287e4c61f630e8da7ba6fd827ce6be85