自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

C#實現(xiàn)AOP微型框架基礎(chǔ)分析

開發(fā) 后端
這里介紹C#實現(xiàn)AOP微型框架,AOP的最基本功能就是實現(xiàn)特定的預(yù)處理和后處理,我通過代理讓C#實現(xiàn)AOP微型框架。先來看看構(gòu)成此微型框架的.cs文件。

在向大家詳細介紹C#實現(xiàn)AOP微型框架之前,首先讓大家了解下微型框架的.cs文件,然后全面介紹C#實現(xiàn)AOP微型框架。

在前面的系列文章中,我介紹了消息、代理與AOP的關(guān)系,這次將我自己用C#實現(xiàn)AOP微型框架拿出來和大家交流一下。

AOP的最基本功能就是實現(xiàn)特定的預(yù)處理和后處理,我通過代理讓C#實現(xiàn)AOP微型框架。先來看看構(gòu)成此微型框架的.cs文件。

1. AopProxyAttribute AOP代理特性

  1. using System;  
  2. using System.Runtime.Remoting ;  
  3. using System.Runtime.Remoting.Proxies ;  
  4.  
  5.  
  6. namespace EnterpriseServerBase.Aop  
  7. {  
  8. /// <summary> 
  9. /// AopProxyAttribute  
  10. /// AOP代理特性,如果一個類想實現(xiàn)具體的AOP,
    只要實現(xiàn)AopProxyBase和IAopProxyFactory,然后加上該特性即可。  
  11. /// 2005.04.11  
  12. /// </summary> 
  13.  
  14. [AttributeUsage(AttributeTargets.Class ,AllowMultiple = false)]  
  15. public class AopProxyAttribute : ProxyAttribute  
  16. {  
  17. private IAopProxyFactory proxyFactory = null ;  
  18.  
  19. public AopProxyAttribute(Type factoryType)  
  20. {  
  21. this.proxyFactory = (IAopProxyFactory)Activator.CreateInstance(factoryType) ;  
  22. }  
  23.  
  24. #region CreateInstance  
  25. /// <summary> 
  26. /// 獲得目標對象的自定義透明代理  
  27. /// </summary> 
  28. public override MarshalByRefObject CreateInstance(Type serverType)
  29. //serverType是被AopProxyAttribute修飾的類  
  30. {  
  31. //未初始化的實例的默認透明代理  
  32. MarshalByRefObject target = base.CreateInstance (serverType);
  33. //得到位初始化的實例(ctor未執(zhí)行)  
  34. object[] args = {target ,serverType} ;  
  35. //AopProxyBase rp = (AopProxyBase)Activator.CreateInstance(this.realProxyType ,args) ; 
  36. //Activator.CreateInstance在調(diào)用ctor時通過了代理,所以此處將會失敗  
  37.  
  38. //得到自定義的真實代理  
  39. AopProxyBase rp = this.proxyFactory.CreateAopProxyInstance(target ,serverType) ;
  40. //new AopControlProxy(target ,serverType) ;  
  41. return (MarshalByRefObject)rp.GetTransparentProxy() ;  
  42. }  
  43. #endregion  
  44. }  

2 .MethodAopSwitcherAttribute.cs

  1. using System;  
  2.  
  3. namespace EnterpriseServerBase.Aop  
  4. {  
  5. /// <summary> 
  6. /// MethodAopSwitcherAttribute 
    用于決定一個被AopProxyAttribute修飾的class的某個特定方法是否啟用截獲 。  
  7. /// 創(chuàng)建原因:絕大多數(shù)時候我們只希望對某個類的一部分Method而不是所有Method使用截獲。  
  8. /// 使用方法:如果一個方法沒有使用MethodAopSwitcherAttribute
    特性或使用MethodAopSwitcherAttribute(false)修飾,  
  9. ///  都不會對其進行截獲。只對使用了MethodAopSwitcherAttribute(true)啟用截獲。  
  10. /// 2005.05.11  
  11. /// </summary> 
  12. [AttributeUsage(AttributeTargets.Method ,AllowMultiple = false )]  
  13. public class MethodAopSwitcherAttribute : Attribute  
  14. {  
  15. private bool useAspect = false ;  
  16.  
  17. public MethodAopSwitcherAttribute(bool useAop)  
  18. {  
  19. this.useAspect = useAop ;  
  20. }  
  21.  
  22. public bool UseAspect  
  23. {  
  24. get  
  25. {  
  26. return this.useAspect ;  
  27. }  
  28. }  
  29. }  

【編輯推薦】

  1. C#創(chuàng)建表單簡單介紹
  2. C#修改DataReader默認行為
  3. C#設(shè)置CooperativeLevel概述
  4. C#表單增加控件簡單描述
  5. C# EmployeePlug類概述
責(zé)任編輯:佚名 來源: 博客園
相關(guān)推薦

2009-09-03 15:03:27

C#實現(xiàn)AOP微型框架

2011-09-15 10:15:30

Spring

2009-09-03 16:17:12

C#操縱系統(tǒng)菜單

2024-06-20 09:58:19

C#Attribute元數(shù)據(jù)機制

2009-09-02 18:03:19

C#實現(xiàn)泛型類

2009-08-28 15:19:17

C#實現(xiàn)縮略圖

2009-08-13 18:02:50

C#基礎(chǔ)概念

2009-08-27 11:43:31

C#語法

2009-08-19 11:21:02

C# ListBox控

2009-08-26 16:46:06

C# ThreadSt

2009-08-27 13:27:50

C# this保留字

2009-08-26 10:34:59

C# Hashtabl

2009-09-01 09:16:57

C#使用SharpZi

2020-08-17 08:20:16

iOSAOP框架

2009-09-03 17:21:51

C# VSProjec

2009-09-03 16:51:27

C#類屬性

2009-09-17 17:44:51

C#動態(tài)數(shù)組

2009-08-27 16:37:06

C#基礎(chǔ)知識

2011-04-25 09:22:44

C#事件

2009-08-13 12:50:45

C#基礎(chǔ)知識
點贊
收藏

51CTO技術(shù)棧公眾號