中国IT动力,最新最全的IT技术教程
最新100篇 | 推荐100篇 | 专题100篇 | 排行榜 | 搜索 | 在线API文档 | 网通镜像
首 页 | 程序开发 | 操作系统 | 软件应用 | 图形图象 | 网络应用 | 精文荟萃 | 教育认证 | 硬件维护 | 未整理篇 | 站长教程
ASP JS PHP工程 ASP.NET 网站建设 UML J2EESUN .NET VC VB VFP 网络维护 数据库 DB2 SQL2000 Oracle Mysql
服务器 Win2000 Office C DreamWeaver FireWorks Flash PhotoShop 上网宝典 CorelDraw 协议大全 网络安全 微软认证
硬件维护  CPU  主板  硬盘  内存  显卡  显示器  键盘鼠标  声卡音箱  打印机  机箱电源  BIOS  网卡  C#  Java  Delphi  vs.net2005
  当前位置:> 程序开发 > 编程语言 > .NET > 临时文章
C# AOP微型框架实现(二)
作者:未知 时间:2005-07-27 21:51 出处:CSDN 责编:chinaitpower
              摘要:C# AOP微型框架实现(二)

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 ;
   }
  }
 }
}


关闭本页
 
首页 | 投资与合作 | 服务条款 | 隐私政策 | 收藏本站 | 设为首页 | 新用户注册 | 免责声明 | 使用帮助
Copyright ©2005-2008 chinaitpower.com All rights reserved. www.chinaitpower.com 版权所有