Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/277.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 拦截接口_C#_Ninject_Interceptor_Convention_Ninject Interception - Fatal编程技术网

C# 拦截接口

C# 拦截接口,c#,ninject,interceptor,convention,ninject-interception,C#,Ninject,Interceptor,Convention,Ninject Interception,我正在尝试制作一个类似于IAuditable接口的东西,它充当Ninject拦截调用的标记 假设我有以下几点: public interface IAuditable { } public interface IProcessor { void Process(object o); } public class Processor : IProcessor, IAuditable { public void Process(object o) { C

我正在尝试制作一个类似于
IAuditable
接口的东西,它充当Ninject拦截调用的标记

假设我有以下几点:

public interface IAuditable
{

}

public interface IProcessor
{
    void Process(object o);
}

public class Processor : IProcessor, IAuditable
{
    public void Process(object o)
    {
        Console.WriteLine("Processor called with argument " + o.ToString());
    }
}
使用此设置:

NinjectSettings settings = new NinjectSettings() { LoadExtensions = true };
IKernel kernel = new StandardKernel(settings);
kernel.Bind<IAuditAggregator>().To<AuditAggregator>().InThreadScope();
kernel.Bind<IAuditInterceptor>().To<AuditInterceptor>();

kernel.Bind(x =>
            x.FromThisAssembly()
            .SelectAllClasses()
            .InheritedFrom<IAuditable>()
            .BindToDefaultInterfaces() //I suspect I need something else here
            .Configure(c => c.Intercept().With<IAuditInterceptor>()));
kernel.Bind<IProcessor>().To<Processor>();

这会导致与使用接口相同的重复绑定问题。

您应该能够为实现IAuditable的类型编写一个约定绑定,为未实现IAuditable的类型编写一个约定绑定

        kernel.Bind(x =>
            x.FromThisAssembly()
                .SelectAllClasses()
                .InheritedFrom<IAuditable>()
                .BindDefaultInterfaces()
                .Configure(c => c.Intercept().With<IAuditInterceptor>()));

        kernel.Bind(x =>
            x.FromThisAssembly()
                .SelectAllClasses()
                .InheritedFrom<IProcessor>()
                .Where(t => !typeof(IAuditable).IsAssignableFrom(t))
                .BindDefaultInterfaces());
kernel.Bind(x=>
x、 来自此程序集()
.SelectAllClasses()
.继承自()
.BindDefaultInterfaces()
.Configure(c=>c.Intercept().With());
Bind(x=>
x、 来自此程序集()
.SelectAllClasses()
.继承自()
.Where(t=>!typeof(IAuditable).IsAssignableFrom(t))
.BindDefaultInterfaces());

我认为在这种情况下,使用[Auditable]属性比使用marker接口要好。我想您可以在
Configure
lambda中编写一条条件语句,仅当类型实现接口(或用属性标记)时才调用
Intercept()
。使用属性会产生相同的结果,会创建重复的绑定。顺便说一句,关于
InThreadScope
的使用:我不确定我是否理解您的问题所在。您使用的是
SelectAllClasses
,由于标记接口或属性的原因,它可以有效地拾取
Processor
。然后在底部“手动”添加另一个绑定,当然它会抱怨。
        kernel.Bind(x =>
            x.FromThisAssembly()
                .SelectAllClasses()
                .InheritedFrom<IAuditable>()
                .BindDefaultInterfaces()
                .Configure(c => c.Intercept().With<IAuditInterceptor>()));

        kernel.Bind(x =>
            x.FromThisAssembly()
                .SelectAllClasses()
                .InheritedFrom<IProcessor>()
                .Where(t => !typeof(IAuditable).IsAssignableFrom(t))
                .BindDefaultInterfaces());