使用代理拦截方法调用例子

从essential .net 第七章抄过来的使用代理拦截方法调用的例子。PriorityProxy用来在方法调用前后提升和回复线程的优先级


ExpandedBlockStart.gif 代码
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;

        }
    }


}


 

转载于:https://www.cnblogs.com/xhan/archive/2010/07/01/1768930.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值