• AOP之PostSharp5LocationInterceptionAspect


        这节我们要讨论的是PostSharp的LocationInterceptionAspect,PostSharp官方把Property和Field成为Location。所以LocationInterceptionAspect就是为了实现Property和Field的拦截。在我们前面讨论了关于方法OnMethodBoundaryAspect的aspect,我们很容易想到,在c#中Property就是一个编译时分为Get和Set两个方法,对于property的aspect就类似于了我们的Method的aspect。而对于Field的aspect同样可以转换为对Property的aspect。

    下面我们用反编译工具来证实一下我的说法.

    代码:

    [LazyLoad("test""test")] 
           private string TestField;

    编译后:

    image

    我们在来看看LocationInterceptionAspect定义:

    image

    其OnGetvalue和OnSetValue是我们主要拦截的方法,起参数LocationInterceptionArgs定义:

    image

    同样给也拥有来自父类AdviceArgs的Instance对象,对于对象级Location为所在对象,静态则为null;

    LocationInterceptionAspect的使用方法和我们的OnMethodBoundaryAspect和类似,使用方式也一样,对于使用对不重要,鄙人觉得更重要的是我们的设计思想。

    我暂时能想到的很好的LocationInterceptionAspect使用场景则是LazyLoad,对于3.5表达式的出现,我们到处都可以简单这个词,在c#类库中也加入了这个类。

    这里我们只是做一个简单的演示demo,根据attribute上制定的类型的方法延时加载对象,废话不说了上code:

    View Code
    [Serializable] 
       public class LazyLoadAttribute : LocationInterceptionAspect 
       { 
           public string MethodName 
           { 
               get
               private set
           } 

           public string PrivoderFullName 
           { 
               get
               private set
           } 

           public LazyLoadAttribute(string MethodName, string PrivoderFullName) 
           { 
               Green.Utility.Guard.ArgumentNotNullOrEmpty(MethodName, "MethodName"); 
               Green.Utility.Guard.ArgumentNotNullOrEmpty(PrivoderFullName, "PrivoderFullName"); 
               this.MethodName = MethodName; 
               this.PrivoderFullName = PrivoderFullName; 
           } 

           public override void OnGetValue(LocationInterceptionArgs args) 
           { 
               if (args.GetCurrentValue() == null
               { 
                   Console.WriteLine("Loading...."); 
                   var value = this.LoadProperty(args.Instance); 
                   if (value != null
                   {                    
                       args.Value = value; 
                       args.ProceedSetValue(); 
                   } 
               } 
               args.ProceedGetValue(); 
           } 

           private object LoadProperty(object p) 
           { 
               var type = Type.GetType(this.PrivoderFullName);//具体加载程序集需要自定义需求,这里仅为了测试简化。 
               if (type != null
               { 
                   var method = type.GetMethod(this.MethodName); 
                   if (method != null
                   { 
                       object[] ps = null
                       if (p != null
                       { 
                           ps = new object[] { p }; 
                       } 
                       object entity = null
                       if (!method.IsStatic) 
                       { 
                           entity = System.Activator.CreateInstance(type); 
                       } 
                       return method.Invoke(entity, ps); 
                   } 
               } 
               return null
           } 
       }
    测试code:
    View Code
    class Program 
       {       
           static void Main(string[] args) 
           {            

               /* 
                * demo4
    */ 

               Student stu = new Student(); 
               stu.ID = 10
               Console.WriteLine(stu.Name); 
               Console.WriteLine(stu.Name); 

               Console.WriteLine(Student.TestStaticProperty); 
               Console.WriteLine(Student.TestStaticProperty); 
               Console.Read(); 
           }

    public static string TextLazyLoadStaticMenthod(Student stu) 
          { 
              return "Student" + stu.ID; 
          } 

          public string TextLazyLoadInstacnceMenthod(Student stu) 
          { 
              return "Student" + stu.ID; 
          } 

          public string TextLazyLoadStaticPropertyMenthod() 
          { 
              return "测试"
          } 
      }

    public class Student 
       { 
          // [LazyLoad("TextLazyLoadStaticMenthod", "PostSharpDemo.Program,PostSharpDemo")] 
           [LazyLoad("TextLazyLoadInstacnceMenthod""PostSharpDemo.Program,PostSharpDemo")] 
           public string Name 
           { getset; } 
           public string Sex 
           { getset; } 

           [LazyLoad("TextLazyLoadStaticPropertyMenthod""PostSharpDemo.Program,PostSharpDemo")] 
           public static string TestStaticProperty 
           { getset; } 

           public int ID 
           { getset; } 
       }
    效果图片如下:

    QQ截图未命名

    附件下载:dmeo

  • AOP之PostSharp初见-OnExceptionAspect
  • AOP之PostSharp2-OnMethodBoundaryAspect
  • AOP之PostSharp3-MethodInterceptionAspect
  • AOP之PostSharp4-实现类INotifyPropertyChanged植入
  • AOP之PostSharp5-LocationInterceptionAspect
  • AOP之PostSharp6-EventInterceptionAspect
  • http://www.cnblogs.com/whitewolf/category/312638.html
  • 相关阅读:
    Atitit 软件知识点分类体系 分类 按照书籍的分类 学科分类 体系与基础部分 计算机体系结构 硬件接口技术(usb,agp,pci,div,hdmi) os操作系统 中间件 语言部分
    Atitit spring注解事务的demo与代码说明 目录 1.1. Spring框架中,要如何实现事务?有一个注解,@EnableTransactionManagement 1 1.2. 事务管理
    Atitit springboot mybatis spring 集成 Springboot1.4 mybatis3.4.6 /springbootMybatis 目录 1.1. 设置map
    Atitit 计算机系统结构 计算机系统结构 Cpu 存储 cache 指令系统 目录 Line 56: 第2章指令系统设计 指令格式 寻址方式 1 Line 64: 第3章CPU及其实现
    Atitit 微服务 分布式 区别 微服务的判断标准 目录 1.1. 区别 微服务侧重于微小服务进程隔离级别,分布式侧重于机器隔离 1 2. 微服务是一种架构, 。多微才叫微? 1 2.1. 微服务
    Atitit spirngboot 访问 html文件总结 自设计web服务器原理与实现 Url路由压力,读取url,获得项目更路径绝对路径,拼接为文件路径。读取文建内容输出即可 目录路径 u
    Atitit 现代信息检索 Atitit 重要章节 息检索建模 检索评价 第8章 文本分类 Line 210: 第9章 索引和搜索 第11章 Web检索 第13章 结构化文本检索 目录 L
    Atitit 定时器timer 总结 目录 1.1. Js定时器 window.setInterval 1 2. Java定时器 timer 1 1.1.Js定时器 window.setInter
    Atitit spring5 集成 mybatis 注解班
    Atitit 微服务的一些理论 目录 1. 微服务的4个设计原则和19个解决方案 1 2. 微服务应用4个设计原则 1 2.1. AKF拆分原则 2 2.2. 前后端分离 2 2.3. 无状态服务
  • 原文地址:https://www.cnblogs.com/whitewolf/p/PostSharp5.html
  • Copyright © 2020-2023  润新知