private static Type TryGetParameterOrReturnType(MethodInfo target, ParameterInfo[] pi, int index) {
if (!target.IsStatic) {
index--;
if (index < 0) {
return target.DeclaringType;
}
}
if (index < pi.Length) {
// next in signature
return pi[index].ParameterType;
}
if (target.ReturnType == typeof(void) || index > pi.Length) {
// no more parameters
return null;
}
// last parameter on Invoke is return type
return target.ReturnType;
}