.NET :消息与AOP(二)

80酷酷网    80kuku.com

  using System;
using System.Runtime.Remoting ;
using System.Runtime.Remoting.Services ;
using System.Runtime.Remoting.Activation ;
using System.Runtime.Remoting.Proxies ;
using System.Runtime.Remoting.Messaging ;

/*
本程序说明了截获是如何进行的,以及真实代理和透明代理之间的关系及交互。
*/
namespace Intercept_example
{
class Class1
{
[STAThread]
static void Main(string[] args)
{
Example exa = new Example("sky" ) ;
exa.say_hello() ;
}
}

//定义一个真实代理,用于实现一个特定的预处理和后处理
public class InterceptProxy : RealProxy
{
private readonly MarshalByRefObject target ;

public InterceptProxy(MarshalByRefObject obj ,Type type) :base(type)
{
this.target = obj ;
}


public override IMessage Invoke(IMessage msg)
{
IMethodCallMessage call = (IMethodCallMessage)msg ;

//如果触发的是构造函数,此时target的构建还未开始
IConstructionCallMessage ctor = call as IConstructionCallMessage ;
if(ctor != null)
{
System.Console.WriteLine("转发构造函数调用!") ;

RealProxy default_proxy = RemotingServices.GetRealProxy(this.target) ;

default_proxy.InitializeServerObject(ctor) ;
MarshalByRefObject tp = (MarshalByRefObject)this.GetTransparentProxy() ;

return EnterpriseServicesHelper.CreateConstructionReturnMessage(ctor,tp);

}

System.Console.WriteLine("预处理中......") ;
System.Threading.Thread.Sleep(5000) ;

IMethodReturnMessage result_msg = RemotingServices.ExecuteMessage(this.target ,call) ;

System.Console.WriteLine("后处理中......") ;
System.Threading.Thread.Sleep(5000) ;
System.Console.WriteLine("后处理结束!") ;

return result_msg ;

}
}



//定义一个特性,该特性可以将上面的真实代理与运用该特性的class联系起来
[AttributeUsage(AttributeTargets.Class)]
public class InterceptProxyAttribute : ProxyAttribute
{
//得到透明代理
public override MarshalByRefObject CreateInstance(Type serverType)
{
//未初始化的实例
MarshalByRefObject target = base.CreateInstance (serverType);

InterceptProxy rp = new InterceptProxy(target ,serverType) ;

return (MarshalByRefObject)rp.GetTransparentProxy() ;
}
}

[InterceptProxy]
public class Example : ContextBoundObject//放到特定的上下文中,该上下文外部才会得到该对象的透明代理
{
private string name ;
public Example(string a)
{
this.name = a ;
}

public void say_hello()
{
Console.WriteLine("hello ! " + name ) ;
}
}

/*
*(1) 当调用方和被调方位于不同的上下文时,调用方得到的是被调对象的透明代理。透明代理会将方法调用打包成一个IMessage对象,并传给实际代理的Invoke方法。
* Invoke方法的默认实现只是将IMessage对象传递到后面的通道(如堆栈生成器等)中。我们可以定制自己的真实代理,重写Invoke方法,为之加上预处理和后处理。
*(2)通过代理特性(如InterceptProxyAttribute)(从ProxyAttribute继承)将真实代理与目标class关联起来。这个代理特性主要重写ProxyAttribute的CreateInstance
* CreateInstance方法,在该方法中,在目标对象的外围包裹上真实代理(如InterceptProxy),而返回该真实代理的透明代理。
* (3)在程序中,不能直接通过真实代理来调用目标对象的方法,因为真实代理处理的是消息对象(IMessage),它是基于消息世界的。
* (4)只有持有透明代理的引用,我们才有可能加入截获(前处理和后处理)。
* (5)派生于ContextBoundObject的类,我们只能得到它的透明代理;派生于MashalByRefObject的类的对象,我们可能得到它的透明代理或者直接引用,这取决于被调方和目
* 标对象是否位于同一上下文中;不从这两个类派生的类的对象,我们只能得到它的直接引用。
*/



}


分享到
  • 微信分享
  • 新浪微博
  • QQ好友
  • QQ空间
点击: