利用消息机制实现.NET AOP(面向方面编程)--通过RealProxy实现 zz
来源:互联网 发布:urlencode java写法 编辑:程序博客网 时间:2024/06/14 07:47
转自 http://www.cnblogs.com/thinhunan/archive/2007/07/17/460370.html
通过RealProxy是最简单的方法,缺点是不能同时加入多个代理,否则会抛异常。
实现步骤:
1、实现RealProxy的继承类
2、实现应用上述实现的RealProxy类的ProxyAttribute
3、把上述实现的Attribute应用到ContextBoundObject实例上
//MyProxy代码using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Services;
using System.Diagnostics;
namespace DotNetAOP.UsingRealProxy
...{
class MyProxy:RealProxy
...{
MarshalByRefObject _target = null;
public MyProxy(Type type, MarshalByRefObject target):base(type)
...{
this._target = target;
}
//覆写Invoke,处理RealProxy截获的各种消息,
//此种方式最简捷,但不能截获远程对象的激活,好在我们并不是真的要Remoting
public override IMessage Invoke(IMessage msg)
...{
IMethodCallMessage call = (IMethodCallMessage)msg;
IConstructionCallMessage ctr = call as IConstructionCallMessage;
IMethodReturnMessage back = null;
//构造函数,只有ContextBoundObject(Inherit from MarshalByRefObject)对象才能截获构造函数
if (ctr != null)
...{
Console.WriteLine("调用"+ctr.ActivationType.Name+"类型的构造函数");
RealProxy defaultProxy = RemotingServices.GetRealProxy(_target);
//如果不做下面这一步,_target还是一个没有直正实例化被代理对象的透明代理,
//这样的话,会导致没有直正构建对象。
defaultProxy.InitializeServerObject(ctr);
//本类是一个RealProxy,它可通过GetTransparentProxy函数得到透明代理
back = EnterpriseServicesHelper.CreateConstructionReturnMessage(ctr, (MarshalByRefObject)GetTransparentProxy());
}
//MarshalByRefObject对象就可截获普通的调用消息,
//MarshalByRefObject对象告诉编译器,不能将其内部简单的成员函数优化成内联代码,
//这样才能保证函数调用都能截获。
else
...{
Console.Write("调用成员函数:" + call.MethodName);
back = RemotingServices.ExecuteMessage(_target, call);
Console.WriteLine(",返回结果为:" + back.ReturnValue.ToString());
}
return back;
}
}
}
using System.Runtime.Remoting;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Services;
using System.Diagnostics;
namespace DotNetAOP.UsingRealProxy
...{
class MyProxy:RealProxy
...{
MarshalByRefObject _target = null;
public MyProxy(Type type, MarshalByRefObject target):base(type)
...{
this._target = target;
}
//覆写Invoke,处理RealProxy截获的各种消息,
//此种方式最简捷,但不能截获远程对象的激活,好在我们并不是真的要Remoting
public override IMessage Invoke(IMessage msg)
...{
IMethodCallMessage call = (IMethodCallMessage)msg;
IConstructionCallMessage ctr = call as IConstructionCallMessage;
IMethodReturnMessage back = null;
//构造函数,只有ContextBoundObject(Inherit from MarshalByRefObject)对象才能截获构造函数
if (ctr != null)
...{
Console.WriteLine("调用"+ctr.ActivationType.Name+"类型的构造函数");
RealProxy defaultProxy = RemotingServices.GetRealProxy(_target);
//如果不做下面这一步,_target还是一个没有直正实例化被代理对象的透明代理,
//这样的话,会导致没有直正构建对象。
defaultProxy.InitializeServerObject(ctr);
//本类是一个RealProxy,它可通过GetTransparentProxy函数得到透明代理
back = EnterpriseServicesHelper.CreateConstructionReturnMessage(ctr, (MarshalByRefObject)GetTransparentProxy());
}
//MarshalByRefObject对象就可截获普通的调用消息,
//MarshalByRefObject对象告诉编译器,不能将其内部简单的成员函数优化成内联代码,
//这样才能保证函数调用都能截获。
else
...{
Console.Write("调用成员函数:" + call.MethodName);
back = RemotingServices.ExecuteMessage(_target, call);
Console.WriteLine(",返回结果为:" + back.ReturnValue.ToString());
}
return back;
}
}
}
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Proxies;
namespace DotNetAOP.UsingRealProxy
{
//从ProxyAttribute继承,自动实现RealProxy植入
[AttributeUsage(AttributeTargets.Class)]
class MyProxyAttribute:ProxyAttribute
{
//覆写CreateInstance函数,返回我们自建的代理
public override MarshalByRefObject CreateInstance(Type serverType)
{
MarshalByRefObject obj = base.CreateInstance(serverType);
MyProxy proxy = new MyProxy(serverType, obj);
return (MarshalByRefObject)proxy.GetTransparentProxy();
}
}
}
using System.Collections.Generic;
using System.Text;
using System.Runtime.Remoting.Proxies;
namespace DotNetAOP.UsingRealProxy
{
//从ProxyAttribute继承,自动实现RealProxy植入
[AttributeUsage(AttributeTargets.Class)]
class MyProxyAttribute:ProxyAttribute
{
//覆写CreateInstance函数,返回我们自建的代理
public override MarshalByRefObject CreateInstance(Type serverType)
{
MarshalByRefObject obj = base.CreateInstance(serverType);
MyProxy proxy = new MyProxy(serverType, obj);
return (MarshalByRefObject)proxy.GetTransparentProxy();
}
}
}
//MyCBO代码
using System;
using System.Collections.Generic;
using System.Text;
namespace DotNetAOP.UsingRealProxy
{
[MyProxy]
class MyCBO:ContextBoundObject
{
public int Add(int a, int b)
{
return a + b;
}
public int Divide(int a, int b)
{
return a / b;
}
}
}
using System.Collections.Generic;
using System.Text;
namespace DotNetAOP.UsingRealProxy
{
[MyProxy]
class MyCBO:ContextBoundObject
{
public int Add(int a, int b)
{
return a + b;
}
public int Divide(int a, int b)
{
return a / b;
}
}
}
//测试代码
using System;
using System.Collections.Generic;
using System.Text;
namespace DotNetAOP
{
class Program
{
static void Main(string[] args)
{
UsingRealProxy.MyCBO cbo = new DotNetAOP.UsingRealProxy.MyCBO();
cbo.Add(1, 2);
Console.ReadLine();
}
}
}
using System.Collections.Generic;
using System.Text;
namespace DotNetAOP
{
class Program
{
static void Main(string[] args)
{
UsingRealProxy.MyCBO cbo = new DotNetAOP.UsingRealProxy.MyCBO();
cbo.Add(1, 2);
Console.ReadLine();
}
}
}
我曾试图实现多个RealProxy嵌套代理对象,不过最后以失败告终,不过后面的两个实现都是可以应用多个截获的。
下载:嵌套RealProxy框架的简单实现 这个框架以失败告终,原因是一个对象不能有多个代理,奇怪的是RemotingProxy又可以,还有待研究。
需要上面单个RealProxy实现的请留下Email。
- 利用消息机制实现.NET AOP(面向方面编程)--通过RealProxy实现 zz
- 利用消息机制实现.NET AOP(面向方面编程)--通过RealProxy实现
- 利用消息机制实现.NET AOP(面向方面编程)
- .NET中通过代理实现面向方面编程(AOP)
- .NET中通过代理实现面向方面编程
- 实现MVC+AOP面向方面编程
- 实现MVC+AOP面向方面编程
- 面向切面编程(3):AOP实现机制
- 面向切面编程(3):AOP实现机制
- 继上次使用remoting实现了AOP,这次是通过RealProxy和ProxyAttribute实现
- AOP---面向方面编程
- AOP面向方面编程
- AOP 面向方面编程
- AOP面向方面编程
- AOP-面向方面编程
- AOP面向方面编程
- AOP面向方面编程
- AOP面向方面编程
- Access键盘快捷键大全
- 在线编辑公文并保存到后台数据库
- 单个图片上传到数据库
- 我的powerbuilder框架,经过三套软件的开发逐渐形成并得到完善
- linux grub confusion
- 利用消息机制实现.NET AOP(面向方面编程)--通过RealProxy实现 zz
- Invalid response document returned from XmlRpc server
- Hibernate 之QBC
- 北大青鸟S1的C语言项目(学员管理系统)
- 一款不错的图像处理软件(含源码)
- 十分钟经济学:让你看懂中国经济形势
- WebService同步两台远程SQLSERVER2000数据库
- 关于hibernate session中的三个状态
- Helix Server和RealProducer Plus 的直播设置