www.pudn.com > Websharp2005.rar > AspectProxy.cs
using System;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Remoting.Activation ;
using System.Runtime.Remoting.Services;
using System.Runtime.Remoting.Channels;
namespace Websharp.Aspect
{
///
/// Aspect代理,在这个类里面,实现对方法的拦截
///
public class AspectProxy : RealProxy
{
///
/// 默认构造函数
///
public AspectProxy() : base()
{
}
///
/// 构造函数
///
/// 被代理的类的类型
public AspectProxy(Type myType) : base(myType)
{
}
///
/// 构造函数
///
/// 被代理的类的类型
/// 被代理的对象
public AspectProxy(Type myType,MarshalByRefObject obj) : base(myType)
{
target=obj;
}
MarshalByRefObject target;
///
/// 当在派生类中重写时,在当前实例所表示的远程对象上调用在所提供的 IMessage 中指定的方法。
/// WebsharpAspect在这里执行对方法执行的拦截处理
///
/// IMessage,包含有关方法调用的信息。
/// 调用的方法所返回的消息,包含返回值和所有 out 或 ref 参数。
public override IMessage Invoke(IMessage msg)
{
PreProcess(msg);
IMessage retMsg;
if (msg is IConstructionCallMessage)
{
IConstructionCallMessage ccm = (IConstructionCallMessage)msg;
RemotingServices.GetRealProxy(target).InitializeServerObject(ccm);
ObjRef oRef = RemotingServices.Marshal(target);
RemotingServices.Unmarshal(oRef);
retMsg = EnterpriseServicesHelper.CreateConstructionReturnMessage
(ccm,(MarshalByRefObject)this.GetTransparentProxy());
}
// else if (msg is IMethodReturnMessage)
// {
// IMethodReturnMessage mcm = (IMethodReturnMessage) msg;
// return mcm;
// }
else
{
IMethodCallMessage mcm = (IMethodCallMessage) msg;
retMsg = RemotingServices.ExecuteMessage(target, mcm);
}
PostProcess(retMsg);
return retMsg;
}
///
/// 被拦截方法执行前进行处理
///
/// IMessage,包含有关方法调用的信息。
private void PreProcess(IMessage msg)
{
IAspect[] aspectBefore=AspectsManager.GetAspect(msg,AspectActionPosition.Before);
if((aspectBefore!=null) && (aspectBefore.Length>0))
{
for(int i=0;i
/// 被拦截方法执行后进行处理
///
/// IMessage,包含有关方法调用的信息。
private void PostProcess(IMessage msg)
{
IAspect[] aspectAfter=AspectsManager.GetAspect(msg,AspectActionPosition.After);
if((aspectAfter!=null) && (aspectAfter.Length>0))
{
for(int i=0;i