1.安装包
autofac
Autofac.Extras.DynamicProxy -- 拦截器扩展
2.具体实现
1 // 1第一种方法 [Intercept(typeof(ActionInterceptor))] 2 class Program 3 { 4 static void Main(string[] args) 5 { 6 var buider = new ContainerBuilder(); 7 // 2第二种方法 8 buider.RegisterType<Child>().As<DBPeople>() 9 .EnableClassInterceptors() 10 .InterceptedBy("action-interceptor"); 11 //.InterceptedBy(typeof(ActionInterceptor)); 12 //注册拦截器 13 //buider.RegisterType<ActionInterceptor>(); 14 //第三种方法 15 buider.RegisterType<ActionInterceptor>().Named<IInterceptor>("action-interceptor"); 16 17 var container = buider.Build(); 18 using (var scope = container.BeginLifetimeScope()) 19 { 20 var child = scope.Resolve<DBPeople>(); 21 child.Say(); 22 } 23 Console.ReadKey(); 24 } 25 } 26 public class ActionInterceptor : IInterceptor 27 { 28 public void Intercept(IInvocation invocation) 29 { 30 Console.WriteLine("say执行前-----------"); 31 invocation.Proceed(); 32 Console.WriteLine("say执行后-----------"); 33 } 34 }