/// <summary> /// 获取相关的Aspects /// </summary> /// <param name="msg">IMessage,包含有关方法调用的信息。</param> /// <param name="position">拦截的位置</param> /// <returns>Aspect数组</returns> public IInjection[] GetAspect(IMessage msg, AspectActionPosition position) { IMethodMessage mmsg = msg as IMethodMessage; List <IInjection> al = new List <IInjection>(); string fullMethodInfo = mmsg.MethodBase.ReflectedType.FullName + "--" + mmsg.MethodName; for (int i = 0; i < _Aspects.Count; i++) { string key = GetMatchHashKey(mmsg, _Aspects[i], position); if (!_AspectsMatch.ContainsKey(key)) { lock (_AspectsMatch) { if (!_AspectsMatch.ContainsKey(key)) { AddAspectMatch(mmsg, _Aspects[i], position); } } } if (_AspectsMatch[key]) { IInjection asp; if (_Aspects[i].DeployModell.Equals("Singleton")) { asp = _Aspects[i].SingletonAspect; } else { asp = Activator.CreateInstance(_Aspects[i].AssemblyName, _Aspects[i].ClassName).Unwrap() as IInjection; } al.Add(asp); } } return((IInjection[])al.ToArray()); }
//添加Aspect和被拦截类的映射。缓存该信息已获得好的性能 private 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[GetMatchHashKey(mmsg, aspectInfo, position)] = match; }
// 获取Aspect和被拦截类的映射的HashKey private string GetMatchHashKey(IMethodMessage mmsg, AspectInfo aspectInfo, AspectActionPosition position) { return(mmsg.MethodBase.ReflectedType.FullName + "--" + mmsg.MethodName + aspectInfo.MatchPattern + position.ToString()); }