【问题标题】:See if Ninject Activation Context bound to Type查看 Ninject Activation Context 是否绑定到 Type
【发布时间】:2015-01-07 19:21:38
【问题描述】:

我正在尝试对使用 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 属性中很容易找到)

【问题讨论】:

    标签: c# ninject ninject-interception


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 2013-09-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-10-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多