1

Okay in unity I'm trying to use Invoke to call a method given in a string format. I can get it to work with no parameters but using parameters it fails, and I can't figure it.

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!";
}

if SetCVar has no parameters its fine other wise I get the following error.

ArgumentException: failed to convert parameters
4

1 回答 1

3

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

return (string)method.Invoke(cmd.obj, new object[]{str});
于 2013-07-10T21:44:26.997 回答