Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/299.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从C中的字符串调用函数#_C#_.net_Php_String_Function Calls - Fatal编程技术网

C# 从C中的字符串调用函数#

C# 从C中的字符串调用函数#,c#,.net,php,string,function-calls,C#,.net,Php,String,Function Calls,我知道在php中,您可以进行如下调用: $function_name = 'hello'; $function_name(); function hello() { echo 'hello'; } 这在.Net中可能吗?是的。你可以使用反射。大概是这样的: Type thisType=this.GetType(); MethodInfo theMethod=thisType.GetMethod(commandstring); 调用方法(这是用户参数); 使用上述代码,调用的方法必须具有访问

我知道在php中,您可以进行如下调用:

$function_name = 'hello';
$function_name();

function hello() { echo 'hello'; }

这在.Net中可能吗?

是的。你可以使用反射。大概是这样的:

Type thisType=this.GetType();
MethodInfo theMethod=thisType.GetMethod(commandstring);
调用方法(这是用户参数);
使用上述代码,调用的方法必须具有访问修饰符
public
。如果调用非公共方法,则需要使用
BindingFlags
参数,例如
BindingFlags.NonPublic | BindingFlags.Instance

Type thisType=this.GetType();
MethodInfo theMethod=此类型
.GetMethod(commandString,BindingFlags.NonPublic | BindingFlags.Instance);
调用方法(这是用户参数);

您可以使用反射调用类实例的方法,执行动态方法调用:

假设在实际实例中有一个名为hello的方法(如下所示):

一个小切线——如果您想解析和评估包含(嵌套)函数的整个表达式字符串,请考虑NCalc(和NuGET)

例如,根据项目文件稍作修改:

// 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());

EvaluateFunction
委托中,您可以调用现有函数。

此代码在我的console.Net应用程序中工作
class Program
{
    static void Main(string[] args)
    {
        string method = args[0]; // get name method
        CallMethod(method);
    }
    
    public static void CallMethod(string method)
    {
        try
        {
            Type type = typeof(Program);
            MethodInfo methodInfo = type.GetMethod(method);
            methodInfo.Invoke(method, null);
        }
        catch(Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
            Console.ReadKey();
        }
    }
    
    public static void Hello()
    {
        string a = "hello world!";
        Console.WriteLine(a);
        Console.ReadKey();
    }
}
// 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());
class Program
{
    static void Main(string[] args)
    {
        string method = args[0]; // get name method
        CallMethod(method);
    }
    
    public static void CallMethod(string method)
    {
        try
        {
            Type type = typeof(Program);
            MethodInfo methodInfo = type.GetMethod(method);
            methodInfo.Invoke(method, null);
        }
        catch(Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
            Console.ReadKey();
        }
    }
    
    public static void Hello()
    {
        string a = "hello world!";
        Console.WriteLine(a);
        Console.ReadKey();
    }
}