using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Security.Permissions;
using System.Text.RegularExpressions;
using Itabby.Product.EfficDevFramework.Library;
using System.Runtime.Remoting;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Channels;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Services;
namespace Project.DAL
{
[AspectManaged(true)]
public class BusinessObject : ContextBoundObject
{
public void Method(object arg)
{
}
}
public enum AspectActionPosition { Before, After, Both }
[AttributeUsage(AttributeTargets.Class)]
[SecurityPermissionAttribute(SecurityAction.Demand, Flags = SecurityPermissionFlag.Infrastructure)]
internal class AspectManagedAttribute : ProxyAttribute
{
private bool aspectManaged;
public AspectManagedAttribute()
{
aspectManaged = true;
}
public AspectManagedAttribute(bool AspectManaged)
{
aspectManaged = AspectManaged;
}
//本方法将在为BusinessObject实例化时被.net框架调用
public override MarshalByRefObject CreateInstance(Type serverType)
{
MarshalByRefObject mobj = base.CreateInstance(serverType);
if (aspectManaged)
{
RealProxy realProxy = new AspectProxy(serverType, mobj);
MarshalByRefObject retobj = realProxy.GetTransparentProxy() as MarshalByRefObject;
return retobj;
}
else
{
return mobj;
}
}
}
//本类型将在BusinessObject实例化时被.net框架调用
internal class AspectProxy : RealProxy
{
public AspectProxy()
: base()
{
}
public AspectProxy(Type myType)
: base(myType)
{
}
public AspectProxy(Type myType, MarshalByRefObject obj)
: base(myType)
{
target = obj;
}
MarshalByRefObject target;
public override IMessage Invoke(IMessage msg)
{
PreProcess(msg);
IMessage retMsg;
if (msg is IConstructionCallMessage)
{
IConstructionCallMessage ccm = (IConstructionCallMessage)msg;
RemotingServices.GetRealProxy(target).InitializeServerObject(ccm);
ObjRef oRef = RemotingServices.Marshal(target);
RemotingServices.Unmarshal(oRef);
retMsg = EnterpriseServicesHelper.CreateConstructionReturnMessage
(ccm, (MarshalByRefObject)this.GetTransparentProxy());
}
else
{
IMethodCallMessage mcm = (IMethodCallMessage)msg;
retMsg = RemotingServices.ExecuteMessage(target, mcm);
}
PostProcess(msg);
return retMsg;
}
private void PreProcess(IMessage msg)
{
IAspect[] aspectBefore = AspectsManager.GetAspect(msg, AspectActionPosition.Before);
if ((aspectBefore != null) && (aspectBefore.Length > 0))
{
for (int i = 0; i < aspectBefore.Length; i++)
{
aspectBefore[i].Execute((msg as IMethodMessage).Args);
}
}
}
private void PostProcess(IMessage msg)
{
IAspect[] aspectAfter = AspectsManager.GetAspect(msg, AspectActionPosition.After);
if ((aspectAfter != null) && (aspectAfter.Length > 0))
{
for (int i = 0; i < aspectAfter.Length; i++)
{
aspectAfter[i].Execute((msg as IMethodMessage).Args);
}
}
}
}
internal class AspectsManager
{
static AspectsManager()
{
}
public static IAspect[] GetAspect(IMessage msg, AspectActionPosition position)
{
IMethodMessage mmsg = msg as IMethodMessage;
//返回EventWatcher实例,可通过AspectActionPosition来分别返回不同的监视对象
return new IAspect[] { new EventWatcher() };
}
}
public interface IAspect
{
void Execute(object[] paramList);
}
public class EventWatcher : IAspect
{
public void Execute(object[] paramList)
{
//获取调用参数
}
}
}
客户端编码:
BusinessObject biz = new BusinessObject();
biz.Method(null);
运行效果:
EventWatcher的Execute方法将被调用4次.