博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
AOP之PostSharp5-LocationInterceptionAspect
阅读量:7066 次
发布时间:2019-06-28

本文共 3114 字,大约阅读时间需要 10 分钟。

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

编译后:

我们在来看看LocationInterceptionAspect定义:

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

同样给也拥有来自父类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 
       { 
get
set; } 
       
public 
string Sex 
       { 
get
set; } 
       [LazyLoad(
"
TextLazyLoadStaticPropertyMenthod
"
"
PostSharpDemo.Program,PostSharpDemo
")] 
       
public 
static 
string TestStaticProperty 
       { 
get
set; } 
       
public 
int ID 
       { 
get
set; } 
   }
效果图片如下:

附件下载:

 

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

你可能感兴趣的文章
计算机
查看>>
文件修改较优方式
查看>>
oracle导入导出exp,imp
查看>>
oracle check if the display variable is set
查看>>
一键部署Openstack R版
查看>>
《JAVA——帮你解决高并发秒杀》
查看>>
国家级期刊发表要求注意事项
查看>>
C文件操作
查看>>
观察转小写的操作-字符函数
查看>>
Oracle查询访问同一表的两个以上索引(二)
查看>>
office 2016 下载地址
查看>>
Go语言之调试
查看>>
Go语言之 unsafe 包之内存布局
查看>>
Spring Cloud Config 入门
查看>>
rhce第二天笔记
查看>>
oneproxy中间件架构及注意事项
查看>>
phpweb解析不当加上传漏洞
查看>>
CentOS自动挂载NTFS分区的U盘或者移动硬盘
查看>>
2018-2019-1 20165226 20165310 20165315 实验二 固件程序设计
查看>>
安装windows后grub的恢复
查看>>