private static Action GetClientAction(object client, string methodName, object request)
{
Type requestType = typeof(AmazonWebServiceRequest);
Type clientType = client.GetType();
MethodInfo method = null;
ParameterInfo[] parameters;
foreach (var m in clientType.GetRuntimeMethods())
{
if (string.Equals(m.Name, methodName, StringComparison.OrdinalIgnoreCase))
{
parameters = m.GetParameters();
if (parameters.Length == 1 && requestType.GetTypeInfo().IsAssignableFrom(parameters[0].ParameterType.GetTypeInfo()))
{
method = m;
break;
}
}
}
if (method == null)
throw new InvalidOperationException("Cannot find method [" + methodName + "]");
parameters = method.GetParameters();
object[] inputs = request == null ? null : new object[] { request };
if (inputs == null)
{
if (parameters.Length == 0)
{
inputs = null;
}
else if (parameters.Length == 1)
{
var input = Activator.CreateInstance(parameters[0].ParameterType);
inputs = new object[] { input };
}
else
{
throw new InvalidOperationException("Method requires more than 1 input");
}
}
Action action = () => method.Invoke(client, inputs);
return action;
}
}