autofac的拦截器
1.定义基础类
public interface DBPeople { void Say(); } public class Child : DBPeople { public void Say() { Console.WriteLine("你好"); } }
2注册并添加拦截器
var builder = new ContainerBuilder(); builder.RegisterType<ActionInterceptor>();//注册拦截器 // 2第二种方法 //注册Child并为其添加拦截器 builder.RegisterType<Child>().As<DBPeople>().InterceptedBy(typeof(ActionInterceptor)).EnableInterfaceInterceptors(); var container = builder.Build(); var child = container.Resolve<DBPeople>(); child.Say(); Console.ReadKey();
3实现效果
需要通过nuget安装两个Package:Autofac
、Autofac.Extras.DynamicProxy
但是我如果写成从抽象类继承就会报错------先记下来吧
public abstract class DBPeople { public abstract void Say(); } public class Child : DBPeople { public override void Say() { Console.WriteLine("你好"); } }