Below is some code I use it to call different methods in a web service. Each method has different parameters and return types, and I just wanted a generic method that I could call for all of them.
More importantly, for every call to the service, I needed to do some further processing to check if the ticket used to identify my call to the service) was expired. If I didn't have a generic method like this, I would need to implement the check with every call:
public static object CallService(string ServiceMethod, object[] ServiceMethodParams)
{
object Result = null;
System.Reflection.MethodInfo mi = MyService.GetType().GetMethod(ServiceMethod);
Result = mi.Invoke(MyService, ServiceMethodParams);
// If the request comes back with a "Ticket expired" header, re-login and attempt to call the method again
if (MyService.TicketHeaderValue.Expired == 1)
{
ServiceLogin();
Result = mi.Invoke(MyService, ServiceMethodParams);
}
return Result;
}