开发者社区> 技术小胖子> 正文

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。

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

代码

 


  1. [LazyLoad("test""test")]   
  2.        private string TestField;  

编译后:

 

我们在来看看LocationInterceptionAspect定义:

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

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

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

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

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


  1. View Code   
  2.  
  3. [Serializable]   
  4.    public class LazyLoadAttribute : LocationInterceptionAspect   
  5.    {   
  6.        public string MethodName   
  7.        {   
  8.            get;   
  9.            private set;   
  10.        }   
  11.  
  12.        public string PrivoderFullName   
  13.        {   
  14.            get;   
  15.            private set;   
  16.        }   
  17.  
  18.        public LazyLoadAttribute(string MethodName, string PrivoderFullName)   
  19.        {   
  20.            Green.Utility.Guard.ArgumentNotNullOrEmpty(MethodName, "MethodName");   
  21.            Green.Utility.Guard.ArgumentNotNullOrEmpty(PrivoderFullName, "PrivoderFullName");   
  22.            this.MethodName = MethodName;   
  23.            this.PrivoderFullName = PrivoderFullName;   
  24.        }   
  25.  
  26.        public override void OnGetValue(LocationInterceptionArgs args)   
  27.        {   
  28.            if (args.GetCurrentValue() == null)   
  29.            {   
  30.                Console.WriteLine("Loading....");   
  31.                var value = this.LoadProperty(args.Instance);   
  32.                if (value != null)   
  33.                {                      
  34.                    args.Value = value;   
  35.                    args.ProceedSetValue();   
  36.                }   
  37.            }   
  38.            args.ProceedGetValue();   
  39.        }   
  40.  
  41.        private object LoadProperty(object p)   
  42.        {   
  43.            var type = Type.GetType(this.PrivoderFullName);//具体加载程序集需要自定义需求,这里仅为了测试简化。   
  44.            if (type != null)   
  45.            {   
  46.                var method = type.GetMethod(this.MethodName);   
  47.                if (method != null)   
  48.                {   
  49.                    object[] ps = null;   
  50.                    if (p != null)   
  51.                    {   
  52.                        ps = new object[] { p };   
  53.                    }   
  54.                    object entity = null;   
  55.                    if (!method.IsStatic)   
  56.                    {   
  57.                        entity = System.Activator.CreateInstance(type);   
  58.                    }   
  59.                    return method.Invoke(entity, ps);   
  60.                }   
  61.            }   
  62.            return null;   
  63.        }   
  64.    }  
  65. 复制代码  

测试code:


  1. View Code   
  2.  
  3. class Program   
  4.    {         
  5.        static void Main(string[] args)   
  6.        {              
  7.  
  8.            /*   
  9.             * demo4*/   
  10.  
  11.            Student stu = new Student();   
  12.            stu.ID = 10;   
  13.            Console.WriteLine(stu.Name);   
  14.            Console.WriteLine(stu.Name);   
  15.  
  16.            Console.WriteLine(Student.TestStaticProperty);   
  17.            Console.WriteLine(Student.TestStaticProperty);   
  18.            Console.Read();   
  19.        }  
  20.  
  21. public static string TextLazyLoadStaticMenthod(Student stu)   
  22.       {   
  23.           return "Student" + stu.ID;   
  24.       }   
  25.  
  26.       public string TextLazyLoadInstacnceMenthod(Student stu)   
  27.       {   
  28.           return "Student" + stu.ID;   
  29.       }   
  30.  
  31.       public string TextLazyLoadStaticPropertyMenthod()   
  32.       {   
  33.           return "测试";   
  34.       }   
  35.   }  
  36.  
  37. public class Student   
  38.    {   
  39.       // [LazyLoad("TextLazyLoadStaticMenthod""PostSharpDemo.Program,PostSharpDemo")]   
  40.        [LazyLoad("TextLazyLoadInstacnceMenthod""PostSharpDemo.Program,PostSharpDemo")]   
  41.        public string Name   
  42.        { get; set; }   
  43.        public string Sex   
  44.        { get; set; }   
  45.  
  46.        [LazyLoad("TextLazyLoadStaticPropertyMenthod""PostSharpDemo.Program,PostSharpDemo")]   
  47.        public static string TestStaticProperty   
  48.        { get; set; }   
  49.  
  50.        public int ID   
  51.        { get; set; }   
  52.    }  
  53. 复制代码 

效果图片如下:

附件下载:dmeo






 本文转自 破狼 51CTO博客,原文链接:http://blog.51cto.com/whitewolfblog/835249,如需转载请自行联系原作者

版权声明:本文内容由阿里云实名注册用户自发贡献,版权归原作者所有,阿里云开发者社区不拥有其著作权,亦不承担相应法律责任。具体规则请查看《阿里云开发者社区用户服务协议》和《阿里云开发者社区知识产权保护指引》。如果您发现本社区中有涉嫌抄袭的内容,填写侵权投诉表单进行举报,一经查实,本社区将立刻删除涉嫌侵权内容。

相关文章
面向切面编程AOP
面向切面编程AOP
33 0
AOP(面向切面)
切面用来干什么:                           在平时的开发中会产生很多的重复代码(每次都要创建)                           业务逻辑代码反而不多
883 0
Aop说明
在xml配置中用到那个标签在开启不然不用,他会报错 1.对接口进行动态代理:创建出来的对象必须转换成接口类型                                          原因他创建的是实现类而不是不是接口所以实现的时候必须转换成接口                     ...
818 0
【ABP杂烩】面向切面编程(AOP)知识总结
原文:【ABP杂烩】面向切面编程(AOP)知识总结 目录 1.存在问题 2.AOP的概念 3.AOP应用范围 3.AOP实现方式 4.应用举例 5.结束语 本文在学习【老张的哲学】系列文章AOP相关章节后,自己归纳总结的笔记。
1054 0
文章
问答
文章排行榜
最热
最新
相关电子书
更多
低代码开发师(初级)实战教程
立即下载
阿里巴巴DevOps 最佳实践手册
立即下载
冬季实战营第三期:MySQL数据库进阶实战
立即下载