gpt4 book ai didi

c# 字符串到我可以从中调用函数的类

转载 作者:太空宇宙 更新时间:2023-11-03 18:45:57 25 4
gpt4 key购买 nike

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

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com