1

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

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 属性中很容易找到)
4

1 回答 1

1

我正在俯瞰 Plan 属性,这似乎有效:

if (x.Plan.Type.GetInterface(typeof(IGetIntercepted).FullName) != null)
{
    return true;
}
于 2015-01-07T19:37:37.343 回答