Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/261.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#_Reflection_Unity3d - Fatal编程技术网

C# 调用问题,无法转换参数

C# 调用问题,无法转换参数,c#,reflection,unity3d,C#,Reflection,Unity3d,好的,在unity中,我尝试使用Invoke调用字符串格式的方法。我可以让它在没有参数的情况下工作,但使用参数它会失败,我无法理解 public string SetCVar(string args) { return "hello"; } public string ParseCmdString(string str) { // Find Cmd string string cmdStr = str.Split(' ')[0]; if(cCmds.Cont

好的,在unity中,我尝试使用Invoke调用字符串格式的方法。我可以让它在没有参数的情况下工作,但使用参数它会失败,我无法理解

public string SetCVar(string args)
{
    return "hello";
}


public string ParseCmdString(string str)
{
    // Find Cmd string
    string cmdStr = str.Split(' ')[0];

    if(cCmds.ContainsKey(cmdStr.ToLower()))
    {
        Cmd cmd = cCmds[cmdStr];

        System.Type         objType = cmd.obj.GetType();
        System.Reflection.MethodInfo method = objType.GetMethod(cmd.method, new System.Type[]{typeof(string)});

        return (string)method.Invoke(objType, new object[]{str});
    }


    return "Command not found!";
}
如果
SetCVar
没有参数,那么从其他方面看,我会得到以下错误

ArgumentException: failed to convert parameters

您的
Invoke
调用应调用实例,而不是类型:

return (string)method.Invoke(cmd.obj, new object[]{str});

啊,对不起,我的眼睛疲劳了-有问题的打字错误应该是str而不是cmd,这是一个字符串,我将编辑这个问题。@PhilCK好的-刚刚编辑以显示我现在看到的下一期;)