private static MethodInfo FindMethod(Type type, string methodName, Type[] typeArgs, Expression[] args, BindingFlags flags)
{
int count = 0;
MethodInfo method = null;
foreach (MethodInfo mi in type.GetMethods(flags))
{
if (mi.Name.Equals(methodName, StringComparison.OrdinalIgnoreCase))
{
MethodInfo moo = ApplyTypeArgs(mi, typeArgs);
if (moo != null && IsCompatible(moo, args))
{
// favor public over non-public methods
if (method == null || (!method.IsPublic && moo.IsPublic))
{
method = moo;
count = 1;
}
// only count it as additional method if they both public or both non-public
else if (method.IsPublic == moo.IsPublic)
{
count++;
}
}
}
}
if (count == 0)
{
if (typeArgs != null && typeArgs.Length > 0)
{
throw Error.GenericMethodWithArgsDoesNotExistOnType(methodName, type);
}
else
{
throw Error.MethodWithArgsDoesNotExistOnType(methodName, type);
}
}
if (count > 1)
throw Error.MethodWithMoreThanOneMatch(methodName, type);
return method;
}