.NET :消息與AOP(二)
2024-07-10 12:59:00
供稿:網友
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的類的對象,我們可能得到它的透明代理或者直接引用,這取決于被調方和目
* 標對象是否位于同一上下文中;不從這兩個類派生的類的對象,我們只能得到它的直接引用。
*/
}