C# 调用问题,无法转换参数
好的,在unity中,我尝试使用Invoke调用字符串格式的方法。我可以让它在没有参数的情况下工作,但使用参数它会失败,我无法理解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
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好的-刚刚编辑以显示我现在看到的下一期;)