• C# unity 的 IInterceptionBehavior实现aop拦截器


    以前项目写过使用unity的 IInterceptionBehavior 实现aop拦截器,时间不多就忘了,项目找不到了,然后呢,写个简单的例子,用的收直接用就行了,简单实用,至于什么用,mvc的attribut属性用法差不多,写过滤器还可以的

     1 using System;
     2 using System.Collections.Generic;
     3 using System.Linq;
     4 using System.Text;
     5 using System.Threading.Tasks;
     6 using Unity;
     7 using Unity.Interception;
     8 using Unity.Interception.ContainerIntegration;
     9 using Unity.Interception.InterceptionBehaviors;
    10 using Unity.Interception.Interceptors.InstanceInterceptors.InterfaceInterception;
    11 using Unity.Interception.PolicyInjection.Pipeline;
    12 
    13 namespace AOP
    14 {
    15     public interface IServiceProvider2
    16     {
    17         string GetServicetest();
    18         string GetServicetest2();
    19     }
    20     public class MyObject2 : IServiceProvider2
    21     {
    22 
    23         public string GetServicetest()
    24         {
    25             return "sttt";
    26         }
    27         public string GetServicetest2()
    28         {
    29             throw new Exception("9999999999999999999");
    30         }
    31     }
    32 
    33     public sealed class MyInterceptionBehavior : IInterceptionBehavior
    34     {
    35         #region IInterceptionBehavior Members
    36 
    37         public Boolean WillExecute
    38         {
    39             get { return true; }
    40         }
    41 
    42         public IEnumerable<Type> GetRequiredInterfaces()
    43         {
    44             return new Type[0];
    45         }
    46 
    47         public IMethodReturn Invoke(IMethodInvocation input, GetNextInterceptionBehaviorDelegate getNext)
    48         {
    49             return getNext()(input, getNext);
    50         }
    51 
    52         #endregion
    53 
    54     
    55     }
    56 
    57 
    58    public class Test
    59     {
    60         public void test()
    61         {
    62             IUnityContainer unityContainer = new UnityContainer();
    63 
    64             unityContainer.AddNewExtension<Interception>();
    65             unityContainer.RegisterType<IServiceProvider2, MyObject2>("MyObject2",
    66             new Interceptor<InterfaceInterceptor>(),
    67             new InterceptionBehavior<MyInterceptionBehavior>()
    68             );
    69 
    70             IServiceProvider2 myObject = unityContainer.Resolve<IServiceProvider2>("MyObject2");
    71             myObject.GetServicetest();
    72             myObject.GetServicetest2();
    73 
    74         }
    75     }
    76 
    77 
    78 
    79    
    80 }

    main 方法里调用一下就可以了

    Test t = new Test();
    t.test();

    至于原理,那肯定用到动态代理了,至于动态代理,怎么实现的,我不太会

     动态代理这个样子:
    var generator = new ProxyGenerator();
    var animal = generator.CreateClassProxy<Animal>(new AnimalInterceptor());

  • 相关阅读:
    职业发展拷问——非科班出身如何才能成为一名合格程序员
    记一个神奇的Bug
    Python多维数组切片
    如何查看数组指针指向数组的所有元素
    RoboMongo命令(版本:Robo 3T 1.1.1)
    Git命令(Git版本:Linux 2.14.3)
    逐点收敛与一致收敛
    廖雪峰Python教程疑问
    The Non-Inverting Amplifier Output Resistance by Adrian S. Nastase [转载]
    2.4G无线射频通信模块nRF24L01+开发笔记(基于MSP430RF6989与STM32f0308)(1.(2)有错误,详见更正)
  • 原文地址:https://www.cnblogs.com/Llh-Forerer2015/p/10310750.html
Copyright © 2020-2023  润新知