1.本地代理调用
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
{
private static string[] gg={"sdf", "fsddx"};
[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(1000) ;
IMethodReturnMessage result_msg = RemotingServices.ExecuteMessage(this.target ,call) ;
System.Console.WriteLine("后处理中......") ;
System.Threading.Thread.Sleep(1000) ;
System.Console.WriteLine("后处理结束!") ;
return result_msg ;
}
}
//定义一个特性,该特性可以将上面的真实代理与运用该特性的class联系起来
[AttributeUsage(AttributeTargets.Class)]
public class InterceptProxyAttribute : ProxyAttribute
{
//得到透明代理
public override MarshalByRefObject CreateInstance(Type serverType)
{
System.Console.WriteLine("创建代理开始!") ;
//未初始化的实例
MarshalByRefObject target = base.CreateInstance (serverType);
InterceptProxy rp = new InterceptProxy(target ,serverType) ;
MarshalByRefObject obj = (MarshalByRefObject)rp.GetTransparentProxy();
System.Console.WriteLine("创建代理结束!") ;
return obj ;
}
}
[InterceptProxy]
public class Example : ContextBoundObject//放到特定的上下文中,该上下文外部才会得到该对象的透明代理
{
private string name ;
public Example(string a)
{
System.Console.WriteLine("开始初始化成员!") ;
this.name = a ;
}
public void say_hello()
{
Console.WriteLine("hello ! " + name ) ;
}
}
}
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
{
private static string[] gg={"sdf", "fsddx"};
[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(1000) ;
IMethodReturnMessage result_msg = RemotingServices.ExecuteMessage(this.target ,call) ;
System.Console.WriteLine("后处理中......") ;
System.Threading.Thread.Sleep(1000) ;
System.Console.WriteLine("后处理结束!") ;
return result_msg ;
}
}
//定义一个特性,该特性可以将上面的真实代理与运用该特性的class联系起来
[AttributeUsage(AttributeTargets.Class)]
public class InterceptProxyAttribute : ProxyAttribute
{
//得到透明代理
public override MarshalByRefObject CreateInstance(Type serverType)
{
System.Console.WriteLine("创建代理开始!") ;
//未初始化的实例
MarshalByRefObject target = base.CreateInstance (serverType);
InterceptProxy rp = new InterceptProxy(target ,serverType) ;
MarshalByRefObject obj = (MarshalByRefObject)rp.GetTransparentProxy();
System.Console.WriteLine("创建代理结束!") ;
return obj ;
}
}
[InterceptProxy]
public class Example : ContextBoundObject//放到特定的上下文中,该上下文外部才会得到该对象的透明代理
{
private string name ;
public Example(string a)
{
System.Console.WriteLine("开始初始化成员!") ;
this.name = a ;
}
public void say_hello()
{
Console.WriteLine("hello ! " + name ) ;
}
}
}
创建代理开始!
//返回透明代理给对象
创建代理结束!
//对象中的透明代理发起调用
转发构造函数调用!
开始初始化成员!
预处理中......
hello ! sky
后处理中......
2.在远程调用中
消息流动:
-- call-->【透明对象】--IMessage-->【实际代理】--IMessage2-->【消息接收器】(在一个调用中,编码 >>自定义前期操作>>传送到远端 >>收到应答>>自定操作后续操作>>解码)
参与对象:
RealProxy,IMessage,IClientChannelSink
http://blog.csdn.net/anghlq/archive/2006/10/31/1358780.aspx