/// <summary> /// 获取目标参数方法顺序 /// </summary> /// <param name="source_type"></param> /// <param name="source_method_name"></param> /// <param name="aop_method_type"></param> /// <returns></returns> private static string[] GetSeachTargetMethodNameOrders(Type source_type, string source_method_name, AOPMethodType aop_method_type) { string[] result = new string[Seach_Format_Target_Method_Name_Orders.Length + 1]; for (int i = 0; i < Seach_Format_Target_Method_Name_Orders.Length; i++) { result[i] = GetTargetMethodName(Seach_Format_Target_Method_Name_Orders[i], source_type, source_method_name, aop_method_type); } // 再加上默认的处理方法 result[Seach_Format_Target_Method_Name_Orders.Length] = aop_method_type.ToString(); return(result); }
/// <summary> /// 获取目标函数 /// </summary> /// <param name="aopAttributeMethodInfoProxyCategory"></param> /// <param name="aopMethodType"></param> /// <param name="sourceMethodOwner"></param> /// <param name="sourceMethodBase"></param> /// <param name="sourceMethodParameterTypes"></param> /// <returns></returns> private MethodInfoProxy GetTargetMethodInfoProxy( AOPAttributeMethodInfoProxyCategory aopAttributeMethodInfoProxyCategory, AOPMethodType aopMethodType, object sourceMethodOwner, MethodBase sourceMethodBase, params Type[] sourceMethodParameterTypes) { //如果缓存中有对应的aop处理函数,则不用查找,直接使用 if (aopAttributeMethodInfoProxyCategory.aopMethodInfoDict.ContainsKey(aopMethodType)) { return(aopAttributeMethodInfoProxyCategory.aopMethodInfoDict[aopMethodType]); } //根据优先顺序查找 var aopAttributeType = aopAttributeMethodInfoProxyCategory.aopAttribute.GetType(); var sourceMethodName = sourceMethodBase.Name; return(AOPUtil.SeachTargetMethodInfoProxy(aopAttributeType, sourceMethodBase.DeclaringType, sourceMethodName, aopMethodType, sourceMethodParameterTypes)); }
/// <summary> /// 查找并调用被切面的方法的其中一个AOPAttribute属性中的AOP处理方法 /// </summary> /// <param name="aopAttributeMethodInfoProxyCategory"></param> /// <param name="aopMethodType"></param> /// <param name="sourceMethodOwner"></param> /// <param name="sourceMethodBase"></param> /// <param name="sourceMethodArgs"></param> private void InvokeAOPAttributeMethod(AOPAttributeMethodInfoProxyCategory aopAttributeMethodInfoProxyCategory, AOPMethodType aopMethodType, object sourceMethodOwner, MethodBase sourceMethodBase, params object[] sourceMethodArgs) { //获取目标函数 var targetMethodInfoProxy = GetTargetMethodInfoProxy(aopAttributeMethodInfoProxyCategory, aopMethodType, sourceMethodOwner, sourceMethodBase, sourceMethodBase.GetParameterTypes()); //缓存目标函数以备下次使用 aopAttributeMethodInfoProxyCategory.aopMethodInfoDict[aopMethodType] = targetMethodInfoProxy; //调用目标函数 var targetMethodInfo = targetMethodInfoProxy.GetTargetMethodInfo(); var targetMethodArgs = GetTargetMethodArgs(targetMethodInfoProxy.isTargetMethodAddSelfArg, sourceMethodOwner, targetMethodInfoProxy.isTargetMethodWithSourceArgs, sourceMethodArgs); targetMethodInfo.Invoke(aopAttributeMethodInfoProxyCategory.aopAttribute, targetMethodArgs); }
/// 特殊情况调用优先顺序 /// 1.被切面的方法的类_被切面的方法的名称_AOPMethodType的类型(【被切面的方法的类本身】+被切面方法的参数) /// 1.1.被切面的方法的名称_AOPMethodType的类型(【被切面的方法的类本身】+被切面方法的参数) /// 2.被切面的方法的类_被切面的方法的名称_AOPMethodType的类型(被切面方法的参数) /// 2.1.被切面的方法的名称_AOPMethodType的类型(被切面方法的参数) /// 3.被切面的方法的类_被切面的方法的名称_AOPMethodType的类型(【被切面的方法的类本身】) /// 3.1.被切面的方法的名称_AOPMethodType的类型(【被切面的方法的类本身】) /// 4.被切面的方法的名称_AOPMethodType的类型() /// 4.1.被切面的方法的类_被切面的方法的名称_AOPMethodType的类型() /// 5.默认的处理方法 public static MethodInfoProxy SeachTargetMethodInfoProxy(Type aopAttributeType, Type sourceType, string sourceMethodName, AOPMethodType aopMethodType, Type[] sourceMethodArgTypes) { //从特殊到一般,注意有顺序先后的查找 var names = GetSeachTargetMethodNameOrders(sourceType, sourceMethodName, aopMethodType); for (var i = 0; i < names.Length; i++) { string targetMethodName = names[i]; for (var j = 0; j < Is_Target_Method_With_Source_ArgTypes_Orders.Length; j++) { bool isTargetMethodWithSoruceArgType = Is_Target_Method_With_Source_ArgTypes_Orders[j]; for (var k = 0; k < Is_Target_Method_Self_Arg_Orders.Length; k++) { bool isTargetMethodSelfArg = Is_Target_Method_Self_Arg_Orders[k]; MethodInfoProxy methodInfoProxy = new MethodInfoProxy(targetMethodName, aopAttributeType, sourceType, isTargetMethodSelfArg, isTargetMethodWithSoruceArgType, sourceMethodArgTypes); MethodInfo targetMethod = aopAttributeType.GetMethodInfo(targetMethodName, BindingFlagsConst.All, methodInfoProxy.methodArgTypesProxy.targetMethodArgTypes); if (targetMethod != null) //命中 { return(methodInfoProxy); } } } } throw new Exception(string.Format("can not find AOPAttributeMethod of Method:{0}->{1} AOPAttribute:{2}", sourceType.ToString(), sourceMethodName, aopAttributeType)); }
/// <summary> /// /// </summary> /// <param name="format_target_method_name"></param> /// <param name="source_type"></param> /// <param name="source_method_name"></param> /// <param name="aopMethodType"></param> /// <returns></returns> private static string GetTargetMethodName(string format_target_method_name, Type source_type, string source_method_name, AOPMethodType aopMethodType) { return(string.Format(format_target_method_name, source_type.GetLastName(), source_method_name, aopMethodType.ToString())); }