在 C# 中从字符串调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/540066/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Calling a function from a string in C#
提问by Jeremy Boyd
I know in php you are able to make a call like:
我知道在 php 中你可以拨打这样的电话:
$function_name = 'hello';
$function_name();
function hello() { echo 'hello'; }
Is this possible in .Net?
这在 .Net 中可能吗?
采纳答案by ottobar
Yes. You can use reflection. Something like this:
是的。您可以使用反射。像这样的东西:
Type thisType = this.GetType();
MethodInfo theMethod = thisType.GetMethod(TheCommandString);
theMethod.Invoke(this, userParameters);
回答by regex
In C#, you can create delegates as function pointers. Check out the following MSDN article for information on usage: http://msdn.microsoft.com/en-us/library/ms173171(VS.80).aspx
在 C# 中,您可以将委托创建为函数指针。查看以下 MSDN 文章以获取有关使用的信息:http://msdn.microsoft.com/en-us/library/ms173171(VS.80).aspx
public static void hello()
{
Console.Write("hello world");
}
/* code snipped */
public delegate void functionPointer();
functionPointer foo = hello;
foo(); // Writes hello world to the console.
回答by BFree
class Program
{
static void Main(string[] args)
{
Type type = typeof(MyReflectionClass);
MethodInfo method = type.GetMethod("MyMethod");
MyReflectionClass c = new MyReflectionClass();
string result = (string)method.Invoke(c, null);
Console.WriteLine(result);
}
}
public class MyReflectionClass
{
public string MyMethod()
{
return DateTime.Now.ToString();
}
}
回答by CMS
You can invoke methods of a class instance using reflection, doing a dynamic method invocation:
您可以使用反射调用类实例的方法,执行动态方法调用:
Suppose that you have a method called hello in a the actual instance (this):
假设您在实际实例 (this) 中有一个名为 hello 的方法:
string methodName = "hello";
//Get the method information using the method info class
MethodInfo mi = this.GetType().GetMethod(methodName);
//Invoke the method
// (null- no parameter for the method call
// or you can pass the array of parameters...)
mi.Invoke(this, null);
回答by drzaus
A slight tangent -- if you want to parse and evaluate an entire expression string which contains (nested!) functions, consider NCalc (http://ncalc.codeplex.com/and nuget)
稍微切线 - 如果您想解析和评估包含(嵌套!)函数的整个表达式字符串,请考虑使用 NCalc(http://ncalc.codeplex.com/和 nuget)
Ex. slightly modified from the project documentation:
前任。从项目文档中稍作修改:
// the expression to evaluate, e.g. from user input (like a calculator program, hint hint college students)
var exprStr = "10 + MyFunction(3, 6)";
Expression e = new Expression(exprString);
// tell it how to handle your custom function
e.EvaluateFunction += delegate(string name, FunctionArgs args) {
if (name == "MyFunction")
args.Result = (int)args.Parameters[0].Evaluate() + (int)args.Parameters[1].Evaluate();
};
// confirm it worked
Debug.Assert(19 == e.Evaluate());
And within the EvaluateFunction
delegate you would call your existing function.
在EvaluateFunction
委托中,您将调用现有函数。
回答by Antonio Leite
In Fact I am working on Windows Workflow 4.5 and I got to find a way to pass a delegate from a statemachine to a method with no success. The only way I got to find was to pass a string with the name of the method I wanted to pass as delegate and to convert the string to a delegate inside the method. Very nice answer. Thanks. Check this link https://msdn.microsoft.com/en-us/library/53cz7sc6(v=vs.110).aspx
事实上,我正在研究 Windows Workflow 4.5,我必须找到一种方法将委托从状态机传递给方法,但没有成功。我找到的唯一方法是传递一个带有我想作为委托传递的方法名称的字符串,并将该字符串转换为方法内部的委托。非常好的答案。谢谢。检查此链接https://msdn.microsoft.com/en-us/library/53cz7sc6(v=vs.110).aspx