gpt4 book ai didi

c# - 执行用户指定的函数/方法

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

我想知道,如果在 .net 中有一个类可以在运行时调用函数/方法...

例如:

输入要调用的方法名:sum(10,20)

我的 Ideia 的代码示例是这样的:

class Program
{
static void sum(int a, int b)
{
Console.WriteLine("{0}", a + b);
}

static void Main(string[] args)
{
Console.Write("Enter with a method name to call: ");
string a = Convert.ToString(Console.ReadLine());

// SAMPLE OF IDEA
ProcessMacro PrM = new ProcessMacro(a);
}
}

那么,有可能做到这一点吗?如果是,我该怎么做?

谢谢。

最佳答案

据我了解你的问题。您需要类似 eval 函数的东西。字符串到代码?正如asawyer所说,可以使用反射

查看

C# Eval Function

关于c# - 执行用户指定的函数/方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11696079/

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