从essential .net 第七章抄过来的使用代理拦截方法调用的例子。PriorityProxy用来在方法调用前后提升和回复线程的优先级
代码
using
System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Proxies;
using System.Threading;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Services;
namespace ConsoleApplication1
{
class Program
{
public static void Main()
{
// 使用工厂方法创建对象的透明代理,目标类型必须继承自MarshalByRefObject
MyCalc1 c = MyCalc1.Create(ThreadPriority.Normal);
var a = c.Add( 1 , 3 );
Console.WriteLine(a);
// 拦截对象创建过程创建对象的透明代理,目标类型必须基础自ContextBoundObject
MyCalc2 c3 = new MyCalc2();
var b = c3.Multiply( 2 , 3 );
Console.WriteLine(b);
Console.ReadKey();
}
}
public class MyCalc1 : MarshalByRefObject
{
public static MyCalc1 Create(ThreadPriority level)
{
MyCalc1 target = new MyCalc1();
PriorityProxy rp = new PriorityProxy(target, typeof (MyCalc1), level);
return (MyCalc1)rp.GetTransparentProxy();
}
private MyCalc1() { }
public double Add( double x, double y) { return x + y; }
public double Multiply( double x, double y) { return x * y; }
}
[PriorityProxy(ThreadPriority.Highest)]
public class MyCalc2 : ContextBoundObject
{
public double Add( double x, double y) { return x + y; }
public double Multiply( double x, double y) { return x * y; }
}
[AttributeUsage(AttributeTargets.Class)]
public class PriorityProxyAttribute : ProxyAttribute
{
ThreadPriority level;
public PriorityProxyAttribute(ThreadPriority level)
{
this .level = level;
}
public override MarshalByRefObject CreateInstance(Type t)
{
// note that we delegate to our base to get an
// uninitialized instance!
MarshalByRefObject target = base .CreateInstance(t);
RealProxy pp = new PriorityProxy(target, t, level);
return (MarshalByRefObject)pp.GetTransparentProxy();
}
}
public class PriorityProxy : RealProxy
{
readonly MarshalByRefObject target;
readonly ThreadPriority level;
public PriorityProxy(MarshalByRefObject target, Type type,ThreadPriority level): base (type)
{
this .target = target;
this .level = level;
}
public override IMessage Invoke(IMessage request)
{
// step 1 : adjust priority
Thread here = Thread.CurrentThread;
ThreadPriority old = here.Priority;
here.Priority = level;
// step 2 : forward call
IMessage response = null ;
IMethodCallMessage call = (IMethodCallMessage)request;
IConstructionCallMessage ctor = call as IConstructionCallMessage;
if (ctor != null ) // 构造函数拦截
{
Console.WriteLine( " ctor before " );
// we are holding a TP, so grab its RP
RealProxy defaultProxy = RemotingServices.GetRealProxy(target);
// ask intermediate RP to invoke constructor
defaultProxy.InitializeServerObject(ctor);
// get OUR TP
MarshalByRefObject tp = (MarshalByRefObject) this .GetTransparentProxy();
// return a message containing our TP as the result of the
// constructor call
response = EnterpriseServicesHelper.CreateConstructionReturnMessage(ctor, tp);
Console.WriteLine( " ctor after " );
}
else // 普通方法拦截
{
Console.WriteLine( " method before " );
response = RemotingServices.ExecuteMessage(target, call);
Console.WriteLine( " method after " );
}
// step 3 : restore old priority
here.Priority = old;
// step 4 : return response message to TP
return response;
}
}
}
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Remoting.Proxies;
using System.Threading;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Services;
namespace ConsoleApplication1
{
class Program
{
public static void Main()
{
// 使用工厂方法创建对象的透明代理,目标类型必须继承自MarshalByRefObject
MyCalc1 c = MyCalc1.Create(ThreadPriority.Normal);
var a = c.Add( 1 , 3 );
Console.WriteLine(a);
// 拦截对象创建过程创建对象的透明代理,目标类型必须基础自ContextBoundObject
MyCalc2 c3 = new MyCalc2();
var b = c3.Multiply( 2 , 3 );
Console.WriteLine(b);
Console.ReadKey();
}
}
public class MyCalc1 : MarshalByRefObject
{
public static MyCalc1 Create(ThreadPriority level)
{
MyCalc1 target = new MyCalc1();
PriorityProxy rp = new PriorityProxy(target, typeof (MyCalc1), level);
return (MyCalc1)rp.GetTransparentProxy();
}
private MyCalc1() { }
public double Add( double x, double y) { return x + y; }
public double Multiply( double x, double y) { return x * y; }
}
[PriorityProxy(ThreadPriority.Highest)]
public class MyCalc2 : ContextBoundObject
{
public double Add( double x, double y) { return x + y; }
public double Multiply( double x, double y) { return x * y; }
}
[AttributeUsage(AttributeTargets.Class)]
public class PriorityProxyAttribute : ProxyAttribute
{
ThreadPriority level;
public PriorityProxyAttribute(ThreadPriority level)
{
this .level = level;
}
public override MarshalByRefObject CreateInstance(Type t)
{
// note that we delegate to our base to get an
// uninitialized instance!
MarshalByRefObject target = base .CreateInstance(t);
RealProxy pp = new PriorityProxy(target, t, level);
return (MarshalByRefObject)pp.GetTransparentProxy();
}
}
public class PriorityProxy : RealProxy
{
readonly MarshalByRefObject target;
readonly ThreadPriority level;
public PriorityProxy(MarshalByRefObject target, Type type,ThreadPriority level): base (type)
{
this .target = target;
this .level = level;
}
public override IMessage Invoke(IMessage request)
{
// step 1 : adjust priority
Thread here = Thread.CurrentThread;
ThreadPriority old = here.Priority;
here.Priority = level;
// step 2 : forward call
IMessage response = null ;
IMethodCallMessage call = (IMethodCallMessage)request;
IConstructionCallMessage ctor = call as IConstructionCallMessage;
if (ctor != null ) // 构造函数拦截
{
Console.WriteLine( " ctor before " );
// we are holding a TP, so grab its RP
RealProxy defaultProxy = RemotingServices.GetRealProxy(target);
// ask intermediate RP to invoke constructor
defaultProxy.InitializeServerObject(ctor);
// get OUR TP
MarshalByRefObject tp = (MarshalByRefObject) this .GetTransparentProxy();
// return a message containing our TP as the result of the
// constructor call
response = EnterpriseServicesHelper.CreateConstructionReturnMessage(ctor, tp);
Console.WriteLine( " ctor after " );
}
else // 普通方法拦截
{
Console.WriteLine( " method before " );
response = RemotingServices.ExecuteMessage(target, call);
Console.WriteLine( " method after " );
}
// step 3 : restore old priority
here.Priority = old;
// step 4 : return response message to TP
return response;
}
}
}