async Task InvokeMethod(string methodName, object invokableObject, object methodArg)
{
try
{
MethodInfo handlerMethod =
invokableObject.GetType()
.GetMethods()
.FirstOrDefault(
x =>
x.Name == methodName &&
x.GetParameters().Any(p => p.ParameterType == methodArg.GetType()));
if (handlerMethod == null) throw new Exception("No matching 'Handle' method found on handler.");
await (Task) handlerMethod.Invoke(invokableObject, new[] {methodArg});
}
catch (AggregateException ex)
{
throw ex.InnerException;
}
catch (TargetInvocationException ex)
{
throw ex.InnerException;
}
catch (Exception ex)
{
throw ex.GetBaseException();
}
}