www.pudn.com > Websharp2005.rar > AspectsManager.cs
using System;
using System.Runtime.Remoting.Messaging;
using System.Runtime.Remoting;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Text.RegularExpressions;
namespace Websharp.Aspect
{
///
/// 管理Aspects的类
///
public class AspectsManager
{
///
/// 所有的Aspect集合
///
static AspectInfoCollection aspects=new AspectInfoCollection();
///
/// Aspect和被拦截类的映射表
///
static Hashtable aspectsMatch=new Hashtable();
///
/// 所有的Aspect集合
///
internal static AspectInfoCollection Aspects
{
get{return aspects;}
}
///
/// 静态构造函数,在这里,读取相关配置信息。
///
static AspectsManager()
{
ConfigurationSettings.GetConfig("Websharp.Aspects");
}
///
/// 获取相关的Aspects
///
/// IMessage,包含有关方法调用的信息。
/// 拦截的位置
/// Aspect数组
public static IAspect[] GetAspect(IMessage msg,AspectActionPosition position)
{
IMethodMessage mmsg = msg as IMethodMessage;
ArrayList al=new ArrayList();
string fullMethodInfo=mmsg.MethodBase.ReflectedType.FullName+"--"+mmsg.MethodName;
for(int i=0;i-1)
{
AddAspect(fullMethodInfo,aspects[i],position,ref al,true);
}
}
}
else
{
for(int i=0;i-1) || (aspects[i].PointCutType.IndexOf("Property")>-1))
{
AddAspect(fullMethodInfo,aspects[i],position,ref al,false);
}
}
}
return (IAspect[]) al.ToArray(typeof(IAspect));
*/
#endregion
}
///
/// 添加Aspect和被拦截类的映射。缓存该信息已获得好的性能
///
/// IMessage,包含有关方法调用的信息。
/// Aspect配置信息
/// 拦截的位置
private static void AddAspectMatch(IMethodMessage mmsg,AspectInfo aspectInfo,AspectActionPosition position)
{
bool match = (aspectInfo.ActionPosition.Equals("Both") ||position.ToString().Equals(aspectInfo.ActionPosition));
if(match)
{
//首先校验是否Match类
if(aspectInfo.MatchClass.IndexOf("*")>-1)
{
string clssName=aspectInfo.MatchClass;
if(clssName.StartsWith("*"))
{
clssName=clssName.Substring(1,clssName.Length-1);
}
match=Regex.IsMatch(mmsg.MethodBase.ReflectedType.FullName,clssName);
}
else
{
match=(mmsg.MethodBase.ReflectedType.FullName.Equals(aspectInfo.MatchClass)
|| mmsg.MethodBase.ReflectedType.Name.Equals(aspectInfo.MatchClass));
}
//校验是否Match方法
if(match)
{
match=mmsg.MethodBase.IsConstructor;
if(match)
{
match = (aspectInfo.PointCutType.IndexOf("Construction")>-1);
}
else
{
match = ((aspectInfo.PointCutType.IndexOf("Method")>-1) || (aspectInfo.PointCutType.IndexOf("Property")>-1));
if(match)
{
if(aspectInfo.MatchMethod.IndexOf("*")>-1)
{
string methodName=aspectInfo.MatchMethod;
if(methodName.StartsWith("*"))
{
methodName=methodName.Substring(1,methodName.Length-1);
}
match=Regex.IsMatch(mmsg.MethodName,methodName);
}
else
{
match=(mmsg.MethodName.Equals(aspectInfo.MatchMethod));
}
}
}
}
}
aspectsMatch.Add(GetMatchHashKey(mmsg,aspectInfo,position),match);
}
///
/// 获取Aspect和被拦截类的映射的HashKey
///
/// IMessage,包含有关方法调用的信息。
/// Aspect配置信息
/// 拦截的位置
/// HashKey
private static string GetMatchHashKey(IMethodMessage mmsg,AspectInfo aspectInfo,AspectActionPosition position)
{
return mmsg.MethodBase.ReflectedType.FullName+"--"+mmsg.MethodName+aspectInfo.MatchPattern+position.ToString();
}
#region 废止的代码
/*
private static void AddAspect(string fullMethodInfo,AspectInfo aspectInfo,AspectActionPosition position,ref ArrayList al,bool isConstruct)
{
if(isConstruct || Regex.IsMatch(fullMethodInfo,aspectInfo.MatchPattern) &&
(aspectInfo.ActionPosition.Equals("Both") ||position.ToString().Equals(aspectInfo.ActionPosition)))
{
IAspect asp;
if(aspectInfo.DeployModell.Equals("Singleton"))
{
asp=aspectInfo.SingletonAspect;
}
else
{
asp=Activator.CreateInstance(aspectInfo.AssemblyName,aspectInfo.ClassName).Unwrap() as IAspect;
}
al.Add(asp);
}
}
*/
#endregion
}
}