简体   繁体   中英

How do I call a method on a static class if I've only got the classes string name?

For example I have a list of strings that happen to be the names of static classes and depending on which string name is selected I just want to use that static class. I know what the method is called on the static class, and it is the same on each class.

If the strings were "x", "y", "z" and the selected string is "z" and the method is called process() , then in my code it would call x.process() in some way, x being the name of an actual static class.

Remember these are static classes.

You would use reflection but include the namespace with the class name.

Type type = Type.GetType("namespace.x");
MethodInfo process = type.GetMethod("process");
process.Invoke(null, null);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM