private static MethodInfo GetMethod(Type type, string methodName, Type[] genericTypes, MethodParameter[] prms, bool isGenericVersion) { ArgumentAssert.IsNotNull(type, "type"); ArgumentAssert.IsNotNull(methodName, "methodName"); genericTypes = genericTypes ?? Type.EmptyTypes; prms = prms ?? MethodParameter.EmptyParameters; MethodKey key = new MethodKey(type, methodName, genericTypes, prms, isGenericVersion); return(_getMethod(key)); }
private static MethodInfo GetMethod(MethodKey key) { MethodInfo result = null; //遍历整个继承树,根据参数数量和类型找到方法 const BindingFlags oneLevelFlags = BindingFlags.DeclaredOnly | BindingFlags.Instance | BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.IgnoreCase | BindingFlags.Static; Type currentType = key.ObjectType; do { MethodInfo[] infos = currentType.GetMethods(oneLevelFlags); foreach (MethodInfo info in infos) { if (info.Name.Equals(key.MethodName, StringComparison.OrdinalIgnoreCase)) { if (key.IsOnlyMathName || (ValidateGenericArguments(info, key.GenericTypes) && ValidateParameters(info, key.Parameters))) { result = info; break; } } } if (result != null) { break; } currentType = currentType.BaseType; } while (currentType != null); if (result == null) { return(result); } if (key.IsGenericVersion) { return(result); } return(key.GenericTypes.Length == 0 ? result : result.MakeGenericMethod(key.GenericTypes)); }