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

后端 未结 5 351
旧时难觅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 11:05

    Here is a basic outline of what you would do:

    1. Scan all the objects in the current AppDomain - find the one that matches what you know the class name to be
    2. Get the static method with the name you know on that object
    3. Dynamically invoke it.

    Edit: This will work if you do not know the namespace of the static class. Otherwise use Daniel Brückner's solution as its much simpler.

提交回复
热议问题