Search code examples
c#ninjectninject-interception

See if Ninject Activation Context bound to Type


I am trying to selectively use interception on types using Ninject. If an implementation implements a specific interface I want to intercept it. How can I check a Ninject Activation Context to see if its target implements an interface?

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
    { }
}
  • Note in this example I want to check Foo, not IFoo. (IFoo is easily found in the Ninject.Activation.Binding.Service property)

Solution

  • I was overlooking the Plan property, this seems to work:

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