• 软件测试技术
  • 软件测试博客
  • 软件测试视频
  • 开源软件测试技术
  • 软件测试论坛
  • 软件测试沙龙
  • 软件测试资料下载
  • 软件测试杂志
  • 软件测试人才招聘
    暂时没有公告

字号: 小 中 大 | 推荐给好友 上一篇 | 下一篇

C# AOP微型框架实现(二)

发布: 2007-6-30 23:38 | 作者: admin | 来源: | 查看: 11次 | 进入软件测试论坛讨论

领测软件测试网  

3. AopProxyAttribute  AOP代理特性

/****************************** AopProxyAttribute   ************************************

using System;
using System.Runtime.Remoting ;
using System.Runtime.Remoting.Proxies ;


namespace EnterpriseServerBase.Aop
{
 /// <summary>
 /// AopProxyAttribute
 /// AOP代理特性,如果一个类想实现具体的AOP,只要实现AopProxyBase和IAopProxyFactory,然后加上该特性即可。
 /// 2005.04.11
 /// </summary>
 
 [AttributeUsage(AttributeTargets.Class ,AllowMultiple = false)]
 public class AopProxyAttribute : ProxyAttribute
 {
  private IAopProxyFactory proxyFactory = null ;

  public AopProxyAttribute(Type factoryType)
  {
   this.proxyFactory = (IAopProxyFactory)Activator.CreateInstance(factoryType) ;   
  }

  #region CreateInstance
  /// <summary>
  /// 获得目标对象的自定义透明代理
  /// </summary>
  public override MarshalByRefObject CreateInstance(Type serverType)//serverType是被AopProxyAttribute修饰的类
  {
   //未初始化的实例的默认透明代理
   MarshalByRefObject target =  base.CreateInstance (serverType); //得到位初始化的实例(ctor未执行)
   object[] args = {target ,serverType} ;
   //AopProxyBase rp = (AopProxyBase)Activator.CreateInstance(this.realProxyType ,args) ; //Activator.CreateInstance在调用ctor时通过了代理,所以此处将会失败
   
   //得到自定义的真实代理
   AopProxyBase rp = this.proxyFactory.CreateAopProxyInstance(target ,serverType) ;//new AopControlProxy(target ,serverType) ;
   return (MarshalByRefObject)rp.GetTransparentProxy() ;
  }
  #endregion
 }
}

4 .MethodAopSwitcherAttribute.cs

/**************************** MethodAopSwitcherAttribute.cs *************************

using System;

namespace EnterpriseServerBase.Aop
{
 /// <summary>
 /// MethodAopSwitcherAttribute 用于决定一个被AopProxyAttribute修饰的class的某个特定方法是否启用截获 。
 /// 创建原因:绝大多数时候我们只希望对某个类的一部分Method而不是所有Method使用截获。
 /// 使用方法:如果一个方法没有使用MethodAopSwitcherAttribute特性或使用MethodAopSwitcherAttribute(false)修饰,
 ///       都不会对其进行截获。只对使用了MethodAopSwitcherAttribute(true)启用截获。
 /// 2005.05.11
 /// </summary>
 [AttributeUsage(AttributeTargets.Method ,AllowMultiple = false )]
 public class MethodAopSwitcherAttribute : Attribute
 {
  private bool useAspect = false ;

  public MethodAopSwitcherAttribute(bool useAop)
  {   
   this.useAspect = useAop ;
  }

  public bool UseAspect
  {
   get
   {
    return this.useAspect ;
   }
  }
 }
}


文章来源于领测软件测试网 https://www.ltesting.net/


关于领测软件测试网 | 领测软件测试网合作伙伴 | 广告服务 | 投稿指南 | 联系我们 | 网站地图 | 友情链接
版权所有(C) 2003-2010 TestAge(领测软件测试网)|领测国际科技(北京)有限公司|软件测试工程师培训网 All Rights Reserved
北京市海淀区中关村南大街9号北京理工科技大厦1402室 京ICP备2023014753号-2
技术支持和业务联系:[email protected] 电话:010-51297073

软件测试 | 领测国际 | ISTQB | ISTQB官网 | TMMi | TMMi认证 | 国际软件测试工程师认证 | 领测软件测试网