[英]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. 如果字符串是“ x”,“ y”,“ z”且所选字符串是“ z”且该方法称为
process()
,那么在我的代码中它将以某种方式调用x.process()
,x为实际静态类的名称。
Remember these are static
classes. 记住这些是
static
类。
You would use reflection but include the namespace
with the class
name. 您将使用反射,但在
namespace
包含class
名称。
Type type = Type.GetType("namespace.x");
MethodInfo process = type.GetMethod("process");
process.Invoke(null, null);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.