查看 Ninject 激活上下文是否绑定到类型



我正在尝试有选择地使用Ninject对类型使用拦截。如果一个实现实现了一个特定的接口,我想拦截它。如何检查 Ninject 激活上下文以查看其目标是否实现了接口?

public partial class MainWindow : Window
{
    public MainWindow()
    {
        InitializeComponent();
        var kernal = new StandardKernel();
        kernal.Bind<IFoo>().To<Foo>();
        kernal.Intercept(x =>
        {
            if (x is an IGetIntercepted)
            {
                return true;
            }
            return false;
        });
    }
    public interface IGetIntercepted
    { }
    public interface IFoo
    { }
    public class Foo : IFoo, IGetIntercepted
    { }
}
  • 请注意,在此示例中,我想检查的是 Foo,而不是 IFoo。(IFoo 很容易在 Ninject.Activation.Binding.Service 属性中找到)

我忽略了 Plan 属性,这似乎有效:

if (x.Plan.Type.GetInterface(typeof(IGetIntercepted).FullName) != null)
{
    return true;
}

最新更新