Reflection does not care about function overloading. Instead use `Type::GetMethods : MethodInfo[]` instead and make a function like this:
public static void SafeInvoke(this MethodInfo[] methods, object target, params object[] args)
{
foreach (var method in methods)
{
var parameters = method.GetParameters();
if (args.Length != parameters.Length)
continue;
// Check if a type does not match
if (parameters.Where((t, i) => args[i].GetType() != t.ParameterType).Any())
continue;
method.Invoke(target, args);
return;
}
throw new ArgumentException();
}
Note: This is not tested
Edit: Alternatively you get use `Type::GetMethod("MyMethodName", new [] {typeof(MyType),})`
↧