我正在尝试使用字符串调用方法,但出现问题:
void make_moviment(string mov,Vector3 new_mov){
GameObject past_panel = GameObject.Find(actual_level.ToString());
Type t = Type.GetType(past_panel.GetComponents<MonoBehaviour>()[0].GetType ().Name);
MethodInfo method = t.GetMethod("get_answer");
method.Invoke(t,new object[] { mov })); <--- PROBLEM HERE
}
总是有与最后一行相关的错误“对象与目标类型不匹配”。你有什么建议?
最佳答案
method.Invoke(t,new object[] { mov }));
和调用一样
t.WhateverTheMethodIs(mov);
但是t
是Type
,不是那个类型的对象。您需要传入对象以调用那里的方法。 (如果方法是静态的,则为 null)。
关于c# - 在 C# 中使用字符串调用方法时出现 "Object does not match target type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41970341/