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

后端 未结 5 349
旧时难觅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:54

    System.Reflection.Assembly info = typeof(System.Environment).Assembly;
    
    Type t = info.GetType("System.Environment");
    MethodInfo m = t.GetMethod("GetFolderPath");
    
    object result = m.Invoke(null, arguments);
    

提交回复
热议问题