C# generic function to invoke a method of class with static information

less than 1 minute read

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
class Invoker<T> where T : class
{
      static void Invoke(T invokeClass, string methodName, object[] args)
      {
         Debug.Assert(invokeClass != null);
         Type type = typeof(T);
         MethodInfo methodInfo = type.GetMethod(methodName);
         try
         {
            methodInfo.Invoke(invokeClass, args);
         }
         catch(Exception e)
         {
            Trace.WriteLine(e.Message);
         }
      }
}