gpt4 book ai didi

c# - 在 C# 中调用类/方法的函数,就像在 php 中一样

转载 作者:太空狗 更新时间:2023-10-30 00:54:24 25 4
gpt4 key购买 nike

我正在迈出使用 C# 和 asp.net 的第一步,我非常喜欢它。现在,我有一个问题...C# 中是否有像 php 中那样调用类/方法的函数?
例如:

$class = array(
"foo", // class name
"bar" // method name
);
$params = array(
"one",
"two"
);
call_user_func_array($class, $params); //execute Foo->bar("one","two");

最佳答案

不,没有内置的东西可以做到这一点。您可以构建一个使用反射执行类似操作的方法,但这似乎是一个寻找问题的解决方案。

void Main()
{
CallUserFuncArray("UserQuery+Foo", "Bar", "One", "Two");
}

// Define other methods and classes here
public class Foo
{
public static void Bar(string a, string b){}
}

public void CallUserFuncArray(string className, string methodName, params object[] args)
{
Type.GetType(className).GetMethod(methodName).Invoke(null, args);
}

关于c# - 在 C# 中调用类/方法的函数,就像在 php 中一样,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13004503/

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