当前位置: 首页 > news >正文

AOP之PostSharp5-LocationInterceptionAspect

    这节我们要讨论的是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:

ExpandedBlockStart.gif
复制代码
[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:
ExpandedBlockStart.gif
复制代码
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

 


作者:破  狼 
出处:http://www.cnblogs.com/whitewolf/ 
本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。该文章也同时发布在我的独立博客中-个人独立博客、博客园--破狼和51CTO--破狼。http://www.cnblogs.com/whitewolf/archive/2011/12/11/PostSharp5.html


相关文章:

  • 如何快速学习一门新技术
  • 模拟实现部分库函数(strcpy,strcmp,strcat,strstr,memcpy,memmove,memset)
  • 组成原理(一):计算机是如何组成的
  • JDK9相比于JDK8,究竟变强了多少
  • Hive之分区(Partitions)和桶(Buckets)
  • 列式存储?OLAP?ClickHouse究竟是何方神圣
  • 分享Open-E DSS V7 应用系列十篇!
  • 基于SpringBoot和BootStrap的全栈论坛网站(附上源码)
  • 我的Java全系列技术博客
  • 闲着无聊造个轮子,开源一个可快速接入的分布式SSO系统
  • https在电子邮件安全解决方案
  • Python生成10个0-100之间的随机数,将其每个数一行写入“随机数.txt”文件,然后从文件中读出十个数,排序后写入“排序.txt”文件。文件路径随意指定。
  • 手游出海必用的三大推广策略
  • Python 实现批量文件改名操作。比如某路径下有“01.txt”“02.txt”“03,txt”,将其统一改为“学生01.txt”“学生02.txt”“学生03.txt”的形式。
  • NodeJs 搭建简单的聊天室
  • 深入了解以太坊
  • JS 中的深拷贝与浅拷贝
  • gf框架之分页模块(五) - 自定义分页
  • Intervention/image 图片处理扩展包的安装和使用
  • jquery ajax学习笔记
  • Laravel 实践之路: 数据库迁移与数据填充
  • Phpstorm怎样批量删除空行?
  • spring学习第二天
  • VirtualBox 安装过程中出现 Running VMs found 错误的解决过程
  • Webpack 4x 之路 ( 四 )
  • 从 Android Sample ApiDemos 中学习 android.animation API 的用法
  • 记录:CentOS7.2配置LNMP环境记录
  • 如何优雅的使用vue+Dcloud(Hbuild)开发混合app
  • 使用 Docker 部署 Spring Boot项目
  • 再次简单明了总结flex布局,一看就懂...
  • Java数据解析之JSON
  • ​2021半年盘点,不想你错过的重磅新书
  • # 日期待t_最值得等的SUV奥迪Q9:空间比MPV还大,或搭4.0T,香
  • $.ajax,axios,fetch三种ajax请求的区别
  • $.type 怎么精确判断对象类型的 --(源码学习2)
  • $var=htmlencode(“‘);alert(‘2“); 的个人理解
  • (NO.00004)iOS实现打砖块游戏(九):游戏中小球与反弹棒的碰撞
  • (附源码)计算机毕业设计SSM疫情居家隔离服务系统
  • (教学思路 C#之类三)方法参数类型(ref、out、parmas)
  • (十八)SpringBoot之发送QQ邮件
  • (一)为什么要选择C++
  • (转) RFS+AutoItLibrary测试web对话框
  • ***原理与防范
  • .cn根服务器被攻击之后
  • .net 微服务 服务保护 自动重试 Polly
  • .Net6支持的操作系统版本(.net8已来,你还在用.netframework4.5吗)
  • .net反混淆脱壳工具de4dot的使用
  • .NET正则基础之——正则委托
  • .net之微信企业号开发(一) 所使用的环境与工具以及准备工作
  • .Net中间语言BeforeFieldInit
  • .sh 的运行
  • [ C++ ] STL---string类的模拟实现
  • [ vulhub漏洞复现篇 ] Apache APISIX 默认密钥漏洞 CVE-2020-13945
  • [100天算法】-不同路径 III(day 73)
  • [acwing周赛复盘] 第 94 场周赛20230311