initialize a class by string variable in c#?上,我已经找到了如何使用字符串创建类的方法

所以我已经拥有的是:

Type type = Type.GetType("project.start");
var class = Activator.CreateInstance(type);


我想做的是例如在此类上调用一个函数:

class.foo();


这可能吗?如果是这样的话?

最佳答案

Type yourType = Type.GetType("project.start");
object yourObject = Activator.CreateInstance(yourType);

object result = yourType.GetMethod("foo")
                        .Invoke(yourObject, null);

关于c# - C#字符串到我可以从中调用函数的类,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/4491514/

10-13 08:19