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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值