public static MethodInfo GetDeclaredMethod(bool tryOnly, Type type, NewType returnType, String methodName, NewType[] parameters) { foreach (MethodInfo method in GetDeclaredMethodsInHierarchy(type)) { if (!method.Name.Equals(methodName)) { continue; } if (returnType != null && !NewType.GetType(method.ReturnType).Equals(returnType)) { continue; } if (parameters == null) { return(method); } ParameterInfo[] currentParameters = method.GetParameters(); if (currentParameters.Length != parameters.Length) { continue; } bool sameParameters = true; for (int a = currentParameters.Length; a-- > 0;) { if (parameters[a] != null && !NewType.GetType(currentParameters[a].ParameterType).Equals(parameters[a])) { sameParameters = false; break; } } if (sameParameters) { return(method); } } if (tryOnly) { return(null); } String propertyName = methodName; if (methodName.ToLowerInvariant().StartsWith("set") || methodName.ToLowerInvariant().StartsWith("get")) { propertyName = StringBuilderUtil.UpperCaseFirst(methodName.Substring(3)); } PropertyInfo propertyInfo; BindingFlags flags = BindingFlags.Public | BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Static | BindingFlags.FlattenHierarchy; if (returnType != null) { try { propertyInfo = type.GetProperty(propertyName, flags, null, returnType.Type, Type.EmptyTypes, new ParameterModifier[0]); } catch (Exception) { throw; } } else { propertyInfo = type.GetProperty(propertyName, flags); } if (propertyInfo != null) { if (methodName.ToLowerInvariant().StartsWith("set") && propertyInfo.GetSetMethod() != null) { return(propertyInfo.GetSetMethod()); } else if (propertyInfo.GetGetMethod() != null) { return(propertyInfo.GetGetMethod()); } } throw new ArgumentException("No matching method found: " + methodName); }
public static MethodInfo GetDeclaredMethod(bool tryOnly, Type type, Type returnType, String methodName, params Type[] parameters) { return(GetDeclaredMethod(tryOnly, type, returnType != null ? NewType.GetType(returnType) : null, methodName, TypeUtil.GetClassesToTypes(parameters))); }