Calling a method sent as a parameter

Last post 06-15-2009 6:26 AM by dihola15. 0 replies.

Sort Posts:

  • Calling a method sent as a parameter

    06-15-2009, 6:26 AM
    • Member
      13 point Member
    • dihola15
    • Member since 04-15-2008, 6:53 AM
    • Posts 98

    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;
        }

Page 1 of 1 (1 items)