现在的位置: 首页 > 综合 > 正文

AOP 类构造函数

2014年01月28日 ⁄ 综合 ⁄ 共 2469字 ⁄ 字号 评论关闭

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Reflection;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Activation;
using System.Windows.Forms;

using System.Runtime.Remoting;
using System.Runtime.Remoting.Services;
using EnterpriseServerBase.Aop.EnterpriseServerBase.Aop;
using EnterpriseServerBase.Aop;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            //Example a = new Example("s");

            //a.say_hello();
            //a.sayByeBye("SSS");

            Aop.AopClass ac = new Aop.AopClass();
            ac.Hello();

            System.Console.Read();
        }

    }
}

namespace Aop
{
    public class AopAttribute : ProxyAttribute
    {
        public override MarshalByRefObject CreateInstance(Type serverType)
        {
            AopProxy realProxy = new AopProxy(serverType);
            return realProxy.GetTransparentProxy() as MarshalByRefObject;
        }

    }
}

namespace Aop
{
    public class AopProxy : RealProxy
    {
        public AopProxy(Type serverType)
            : base(serverType) { }

        public override IMessage Invoke(IMessage msg)
        {
            if (msg is IConstructionCallMessage)
            {
                IConstructionCallMessage constructCallMsg = msg as IConstructionCallMessage;
                IConstructionReturnMessage constructionReturnMessage = this.InitializeServerObject((IConstructionCallMessage)msg);
                RealProxy.SetStubData(this, constructionReturnMessage.ReturnValue);
                MessageBox.Show("Call constructor");
                return constructionReturnMessage;
            }
            else
            {
                IMethodCallMessage callMsg = msg as IMethodCallMessage;
                IMessage message;
                try
                {
                    object[] args = callMsg.Args;
                    object o = callMsg.MethodBase.Invoke(GetUnwrappedServer(), args);
                    message = new ReturnMessage(o, args, args.Length, callMsg.LogicalCallContext, callMsg);
                }
                catch (Exception e)
                {
                    message = new ReturnMessage(e, callMsg);
                }
                MessageBox.Show(message.Properties["__Return"].ToString());
                return message;
            }
        }
    }
}

namespace Aop
{
    [AopAttribute]
    public class AopClass : ContextBoundObject
    {
        public AopClass()
        {
            System.Console.WriteLine("SS");
        }

        public string Hello()
        {
            return "welcome";
        }

    }
}

抱歉!评论已关闭.