using System; using System.Collections.Generic; using System.Text; using System.Runtime.Remoting.Proxies; namespace Aop { public class AopAttribute : ProxyAttribute { public override MarshalByRefObject CreateInstance(Type serverType) { AopProxy realProxy = new AopProxy(serverType); return realProxy.GetTransparentProxy() as MarshalByRefObject; } } }
AopProxy.cs
using System; using System.Collections.Generic; using System.Text; using System.Reflection.Emit; using System.Runtime.Remoting.Proxies; using System.Runtime.Remoting.Messaging; using System.Runtime.Remoting.Activation; using System.Windows.Forms; 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; } } } }
AopAttribute.cs
using System.Collections.Generic; using System.Text; namespace Aop { [AopAttribute] public class AopClass : ContextBoundObject { public string Hello() { return "welcome"; } } }
-----
重点就是这里拦截之后的代码,稍为复杂点,一般照抄就行了,拦截的代码如下:
if (msg is IConstructionCallMessage) // 如果是构造函数,按原来的方式返回即可。 { IConstructionCallMessage constructCallMsg = msg as IConstructionCallMessage; IConstructionReturnMessage constructionReturnMessage = this.InitializeServerObject((IConstructionCallMessage)msg); RealProxy.SetStubData(this, constructionReturnMessage.ReturnValue); return constructionReturnMessage; } else if (msg is IMethodCallMessage) //如果是方法调用(属性也是方法调用的一种) { IMethodCallMessage callMsg = msg as IMethodCallMessage; object[] args = callMsg.Args; IMessage message; try { if (callMsg.MethodName.StartsWith("set_") && args.Length == 1) { //这里检测到是set方法,然后应怎么调用对象的其它方法呢? } 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); } return message; } return msg;
OK , 具体
1 , 应用 在要拦截的类头上加个属性标识,同时继承自ContextBoundObject:
[AopAttribute] public class OrmBase:ContextBoundObject
PS : OrmBase需要拦截
2 , AopAttribute继承代理属性标识类,用来挂在要拦截的类的头上:
class AopAttribute : ProxyAttribute { public override MarshalByRefObject CreateInstance(Type serverType) { AopProxy realProxy = new AopProxy(serverType); return realProxy.GetTransparentProxy() as MarshalByRefObject; } }
3 , AopProxy类,就是拦截的消息处理,先上个简单版,免的大伙看不懂:
class AopProxy : RealProxy { public AopProxy(Type serverType) : base(serverType) { } public override IMessage Invoke(IMessage msg) { //消息拦截之后,就会执行这里的方法。 // 上面有拦截代码 } }
原文 : http://blog.csdn.net/problc/article/details/4152082
http://www.cnblogs.com/cyq1162/archive/2012/05/30/2526573.html