Calling a method on a static class given its type name and method names as strings

后端 未结 5 342
旧时难觅i
旧时难觅i 2020-12-08 10:15

How could I go about calling a method on a static class given the class name and the method name, please?

For example:

Given System.Environment

5条回答
  •  醉梦人生
    2020-12-08 10:50

    Just

    Type.GetType(typeName).GetMethod(methodName).Invoke(null, arguments);
    

    where typeName is the name of the type as a string, methodName is the name of the method as a string, and arguments is an array of objects containing the arguments to call the method with.

提交回复
热议问题