利用消息机制实现.NET AOP(面向方面编程)--利用ServerContextSink实现多截获
以下代码实现利用ServerContextSink来实现多截获
//框架代码
//ObjectWithAspect.cs
using System;
namespace DotNetAOP.ServerContextSink.Framework
{
[ObjectSinkChain]
public abstract class ObjectWithAspects:ContextBoundObject
{
}
}
namespace DotNetAOP.ServerContextSink.Framework
{
[ObjectSinkChain]
public abstract class ObjectWithAspects:ContextBoundObject
{
}
}
//ServerContextSinkChainProperty.cs
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Services;
namespace DotNetAOP.ServerContextSink.Framework
{
[Serializable]
class ServerContextSinkChainProperty:
IContextProperty,
//IContributeObjectSink, //如果不需捕获构造函数,则可使用ObjectSink
IContributeServerContextSink //如果构造函数也要捕获,则使用ServerContextSink
{
string _name;
Type _type;
public ServerContextSinkChainProperty(Type type,string name)
{
_name = name;
_type = type;
}
private AspectAttribute[] GetAspectAttributes(Type type)
{
return (AspectAttribute[])_type.GetCustomAttributes(typeof(AspectAttribute), true);
}
IContextProperty 成员
IContributeObjectSink 成员
IContributeServerContextSink 成员
}
}
using System.Runtime.Remoting;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Services;
namespace DotNetAOP.ServerContextSink.Framework
{
[Serializable]
class ServerContextSinkChainProperty:
IContextProperty,
//IContributeObjectSink, //如果不需捕获构造函数,则可使用ObjectSink
IContributeServerContextSink //如果构造函数也要捕获,则使用ServerContextSink
{
string _name;
Type _type;
public ServerContextSinkChainProperty(Type type,string name)
{
_name = name;
_type = type;
}
private AspectAttribute[] GetAspectAttributes(Type type)
{
return (AspectAttribute[])_type.GetCustomAttributes(typeof(AspectAttribute), true);
}
IContextProperty 成员
IContributeObjectSink 成员
IContributeServerContextSink 成员
}
}
//ServerContextSinkChainAttribute.cs
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Services;
namespace DotNetAOP.ServerContextSink.Framework
{
[AttributeUsage(AttributeTargets.Class)]
class ObjectSinkChainAttribute:ContextAttribute
{
public ObjectSinkChainAttribute()
: base("ServerContextSink_Framework_ObjectSinkChainAttribute")
{
}
public override void GetPropertiesForNewContext(IConstructionCallMessage ctorMsg)
{
Type type = ctorMsg.ActivationType;
ctorMsg.ContextProperties.Add(new ServerContextSinkChainProperty(type,this.AttributeName));
}
public override bool IsContextOK(Context ctx, IConstructionCallMessage ctorMsg)
{
return (ctx.GetProperty(this.AttributeName) != null );
}
}
}
using System.Runtime.Remoting;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Services;
namespace DotNetAOP.ServerContextSink.Framework
{
[AttributeUsage(AttributeTargets.Class)]
class ObjectSinkChainAttribute:ContextAttribute
{
public ObjectSinkChainAttribute()
: base("ServerContextSink_Framework_ObjectSinkChainAttribute")
{
}
public override void GetPropertiesForNewContext(IConstructionCallMessage ctorMsg)
{
Type type = ctorMsg.ActivationType;
ctorMsg.ContextProperties.Add(new ServerContextSinkChainProperty(type,this.AttributeName));
}
public override bool IsContextOK(Context ctx, IConstructionCallMessage ctorMsg)
{
return (ctx.GetProperty(this.AttributeName) != null );
}
}
}
//AsyncReplyHelperSink.cs
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
namespace DotNetAOP.ServerContextSink.Framework
{
class AsyncReplyHelperSink : IMessageSink
{
public delegate IMessage AsyncReplyHelperSinkDelegate(IMessage msg);
IMessageSink _nextSink = null;
AsyncReplyHelperSinkDelegate _delegate = null;
public AsyncReplyHelperSink(IMessageSink nextSink, AsyncReplyHelperSinkDelegate delegates)
{
_nextSink = nextSink;
_delegate = delegates;
}
IMessageSink 成员
}
}
using System.Runtime.Remoting;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
namespace DotNetAOP.ServerContextSink.Framework
{
class AsyncReplyHelperSink : IMessageSink
{
public delegate IMessage AsyncReplyHelperSinkDelegate(IMessage msg);
IMessageSink _nextSink = null;
AsyncReplyHelperSinkDelegate _delegate = null;
public AsyncReplyHelperSink(IMessageSink nextSink, AsyncReplyHelperSinkDelegate delegates)
{
_nextSink = nextSink;
_delegate = delegates;
}
IMessageSink 成员
}
}
//AspectAttribute.cs
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Services;
namespace DotNetAOP.ServerContextSink.Framework
{
public abstract class AspectAttribute:Attribute
{
public abstract IMessageSink CreateAspectSink(IMessageSink nextSink);
}
}
using System.Runtime.Remoting;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Services;
namespace DotNetAOP.ServerContextSink.Framework
{
public abstract class AspectAttribute:Attribute
{
public abstract IMessageSink CreateAspectSink(IMessageSink nextSink);
}
}
//Demo代码
//Program.cs
using System;
using DotNetAOP.ServerContextSink.Framework;
namespace DotNetAOP.ServerContextSink.Demo
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Calculator calc = new Calculator();
Console.WriteLine(calc.Add(1,2));
Console.WriteLine(calc.Divide(2,0));
// Test2 t2 = new Test2();
// t2.Te();
Console.ReadLine();
}
}
}
using DotNetAOP.ServerContextSink.Framework;
namespace DotNetAOP.ServerContextSink.Demo
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class Program
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
Calculator calc = new Calculator();
Console.WriteLine(calc.Add(1,2));
Console.WriteLine(calc.Divide(2,0));
// Test2 t2 = new Test2();
// t2.Te();
Console.ReadLine();
}
}
}
//LogSinkAttribute.cs
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Services;
using DotNetAOP.ServerContextSink.Framework;
namespace DotNetAOP.ServerContextSink.Demo
{
[AttributeUsage(AttributeTargets.Class)]
public class LogSinkAttribute:AspectAttribute
{
public override IMessageSink CreateAspectSink(IMessageSink nextSink)
{
return new LogSink(nextSink);
}
}
}
using System.Runtime.Remoting;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Services;
using DotNetAOP.ServerContextSink.Framework;
namespace DotNetAOP.ServerContextSink.Demo
{
[AttributeUsage(AttributeTargets.Class)]
public class LogSinkAttribute:AspectAttribute
{
public override IMessageSink CreateAspectSink(IMessageSink nextSink)
{
return new LogSink(nextSink);
}
}
}
//LogSink.cs
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Services;
using DotNetAOP.ServerContextSink.Framework;
namespace DotNetAOP.ServerContextSink.Demo
{
public class LogSink:IMessageSink
{
readonly IMessageSink _nextSink;
public LogSink(IMessageSink nextSink)
{
_nextSink = nextSink;
}
IMessageSink 成员
private IMessage AsyncProcessMessage(IMessage msg)
{
IMethodCallMessage call = msg as IMethodCallMessage;
IConstructionCallMessage ctr = call as IConstructionCallMessage;
if (ctr != null)
{
Console.WriteLine("构造函数:\t" + ctr.MethodName);
}
else
{
Console.WriteLine("普通方法:\t" + call.MethodName);
}
return msg;
}
}
}
using System.Runtime.Remoting;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Services;
using DotNetAOP.ServerContextSink.Framework;
namespace DotNetAOP.ServerContextSink.Demo
{
public class LogSink:IMessageSink
{
readonly IMessageSink _nextSink;
public LogSink(IMessageSink nextSink)
{
_nextSink = nextSink;
}
IMessageSink 成员
private IMessage AsyncProcessMessage(IMessage msg)
{
IMethodCallMessage call = msg as IMethodCallMessage;
IConstructionCallMessage ctr = call as IConstructionCallMessage;
if (ctr != null)
{
Console.WriteLine("构造函数:\t" + ctr.MethodName);
}
else
{
Console.WriteLine("普通方法:\t" + call.MethodName);
}
return msg;
}
}
}
//Log2Sink.cs
using System;
using System.Runtime.Remoting;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Services;
using DotNetAOP.ServerContextSink.Framework;
namespace DotNetAOP.ServerContextSink.Demo
{
public class Log2Sink : IMessageSink
{
readonly IMessageSink _nextSink;
public Log2Sink(IMessageSink nextSink)
{
_nextSink = nextSink;
}
IMessageSink 成员
private IMessage AsyncProcessMessage(IMessage msg)
{
IMethodCallMessage call = msg as IMethodCallMessage;
IConstructionCallMessage ctr = call as IConstructionCallMessage;
if (ctr != null)
{
Console.WriteLine("构造函数:\t" + ctr.MethodName);
}
else
{
Console.WriteLine("普通方法:\t" + call.MethodName);
}
return msg;
}
}
[AttributeUsage(AttributeTargets.Class)]
public class Log2SinkAttribute : AspectAttribute
{
public override IMessageSink CreateAspectSink(IMessageSink nextSink)
{
return new Log2Sink(nextSink);
}
}
}
using System.Runtime.Remoting;
using System.Runtime.Remoting.Activation;
using System.Runtime.Remoting.Contexts;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting.Proxies;
using System.Runtime.Remoting.Services;
using DotNetAOP.ServerContextSink.Framework;
namespace DotNetAOP.ServerContextSink.Demo
{
public class Log2Sink : IMessageSink
{
readonly IMessageSink _nextSink;
public Log2Sink(IMessageSink nextSink)
{
_nextSink = nextSink;
}
IMessageSink 成员
private IMessage AsyncProcessMessage(IMessage msg)
{
IMethodCallMessage call = msg as IMethodCallMessage;
IConstructionCallMessage ctr = call as IConstructionCallMessage;
if (ctr != null)
{
Console.WriteLine("构造函数:\t" + ctr.MethodName);
}
else
{
Console.WriteLine("普通方法:\t" + call.MethodName);
}
return msg;
}
}
[AttributeUsage(AttributeTargets.Class)]
public class Log2SinkAttribute : AspectAttribute
{
public override IMessageSink CreateAspectSink(IMessageSink nextSink)
{
return new Log2Sink(nextSink);
}
}
}
//Calculator.cs
using System;
using DotNetAOP.ServerContextSink.Framework;
namespace DotNetAOP.ServerContextSink.Demo
{
[LogSink,Log2Sink]
public class Calculator:ObjectWithAspects
{
public Calculator()
{
}
public double Add(double a , double b)
{
return a + b;
}
public double Divide(double a, double b)
{
return a/b;
}
}
}
using DotNetAOP.ServerContextSink.Framework;
namespace DotNetAOP.ServerContextSink.Demo
{
[LogSink,Log2Sink]
public class Calculator:ObjectWithAspects
{
public Calculator()
{
}
public double Add(double a , double b)
{
return a + b;
}
public double Divide(double a, double b)
{
return a/b;
}
}
}
需要上面完整代码请留下Email。
查看其它框架实现请转到此页
利用消息机制实现.NET AOP(面向方面编程)--通过RealProxy实现
利用消息机制实现.NET AOP(面向方面编程)--利用RealProxy和消息接收器实现多截获
利用消息机制实现.NET AOP(面向方面编程)--利用ServerContextSink实现多截获