使用代理攔截方法調用例子

來源:互聯網
上載者:User

從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()
        {
            //使用Factory 方法建立對象的透明代理,目標類型必須繼承自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;

        }
    }

}

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.