利用消息机制实现.NET AOP(面向方面编程)--通过RealProxy实现

通过RealProxy是最简单的方法,缺点是不能同时加入多个代理,否则会抛异常。

实现步骤:

1、实现RealProxy的继承类

2、实现应用上述实现的RealProxy类的ProxyAttribute

3、把上述实现的Attribute应用到ContextBoundObject实例上

None.gif // MyProxy代码
None.gif using  System;
None.gif
using  System.Runtime.Remoting;
None.gif
using  System.Runtime.Remoting.Messaging;
None.gif
using  System.Runtime.Remoting.Proxies;
None.gif
using  System.Runtime.Remoting.Activation;
None.gif
using  System.Runtime.Remoting.Services;
None.gif
using  System.Diagnostics;
None.gif
None.gif
namespace  DotNetAOP.UsingRealProxy
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
class MyProxy:RealProxy
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        MarshalByRefObject _target 
= null;
InBlock.gif
InBlock.gif        
public MyProxy(Type type, MarshalByRefObject target):base(type)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
this._target = target;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
//覆写Invoke,处理RealProxy截获的各种消息,
InBlock.gif        
//此种方式最简捷,但不能截获远程对象的激活,好在我们并不是真的要Remoting
InBlock.gif
        public override IMessage Invoke(IMessage msg)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            IMethodCallMessage call 
= (IMethodCallMessage)msg;
InBlock.gif            IConstructionCallMessage ctr 
= call as IConstructionCallMessage;
InBlock.gif
InBlock.gif            IMethodReturnMessage back 
= null;
InBlock.gif
InBlock.gif            
//构造函数,只有ContextBoundObject(Inherit from MarshalByRefObject)对象才能截获构造函数
InBlock.gif
            if (ctr != null)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.WriteLine(
"调用"+ctr.ActivationType.Name+"类型的构造函数");
InBlock.gif
InBlock.gif                RealProxy defaultProxy 
= RemotingServices.GetRealProxy(_target);
InBlock.gif
InBlock.gif                
//如果不做下面这一步,_target还是一个没有直正实例化被代理对象的透明代理,
InBlock.gif                
//这样的话,会导致没有直正构建对象。
InBlock.gif
                defaultProxy.InitializeServerObject(ctr);
InBlock.gif
InBlock.gif                
//本类是一个RealProxy,它可通过GetTransparentProxy函数得到透明代理
InBlock.gif
                back = EnterpriseServicesHelper.CreateConstructionReturnMessage(ctr, (MarshalByRefObject)GetTransparentProxy());
ExpandedSubBlockEnd.gif            }

InBlock.gif            
//MarshalByRefObject对象就可截获普通的调用消息,
InBlock.gif            
//MarshalByRefObject对象告诉编译器,不能将其内部简单的成员函数优化成内联代码,
InBlock.gif            
//这样才能保证函数调用都能截获。
InBlock.gif
            else
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                Console.Write(
"调用成员函数:" + call.MethodName);
InBlock.gif
InBlock.gif                back 
= RemotingServices.ExecuteMessage(_target, call);
InBlock.gif
InBlock.gif                Console.WriteLine(
",返回结果为:" + back.ReturnValue.ToString());
ExpandedSubBlockEnd.gif            }

InBlock.gif
InBlock.gif            
return back;
InBlock.gif
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif // MyProxyAttribute
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
using  System.Runtime.Remoting.Proxies;
None.gif
None.gif
namespace  DotNetAOP.UsingRealProxy
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
//从ProxyAttribute继承,自动实现RealProxy植入
InBlock.gif
    [AttributeUsage(AttributeTargets.Class)]
InBlock.gif    
class MyProxyAttribute:ProxyAttribute
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif
InBlock.gif        
//覆写CreateInstance函数,返回我们自建的代理
InBlock.gif
        public override MarshalByRefObject CreateInstance(Type serverType)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            MarshalByRefObject obj 
= base.CreateInstance(serverType);
InBlock.gif            MyProxy proxy 
= new MyProxy(serverType, obj);
InBlock.gif            
return (MarshalByRefObject)proxy.GetTransparentProxy();
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif // MyCBO代码
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
None.gif
namespace  DotNetAOP.UsingRealProxy
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    [MyProxy]
InBlock.gif    
class MyCBO:ContextBoundObject
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
public int Add(int a, int b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return a + b;
ExpandedSubBlockEnd.gif        }

InBlock.gif        
public int Divide(int a, int b)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
return a / b;
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif

None.gif // 测试代码
None.gif using  System;
None.gif
using  System.Collections.Generic;
None.gif
using  System.Text;
None.gif
None.gif
namespace  DotNetAOP
ExpandedBlockStart.gifContractedBlock.gif
dot.gif {
InBlock.gif    
class Program
ExpandedSubBlockStart.gifContractedSubBlock.gif    
dot.gif{
InBlock.gif        
static void Main(string[] args)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            UsingRealProxy.MyCBO cbo 
= new DotNetAOP.UsingRealProxy.MyCBO();
InBlock.gif            cbo.Add(
12);
InBlock.gif            Console.ReadLine();
ExpandedSubBlockEnd.gif        }

ExpandedSubBlockEnd.gif    }

ExpandedBlockEnd.gif}

None.gif


我曾试图实现多个RealProxy嵌套代理对象,不过最后以失败告终,不过后面的两个实现都是可以应用多个截获的。14.gif


下载:嵌套RealProxy框架的简单实现 这个框架以失败告终,原因是一个对象不能有多个代理,奇怪的是RemotingProxy又可以,还有待研究。
需要上面单个RealProxy实现的请留下Email。

查看其它框架实现请转到此页lucky.gif

利用消息机制实现.NET AOP(面向方面编程)--通过RealProxy实现
利用消息机制实现.NET AOP(面向方面编程)--利用RealProxy和消息接收器实现多截获
利用消息机制实现.NET AOP(面向方面编程)--利用ServerContextSink实现多截获

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值