c# - 在 C# 中使用字符串调用方法时出现 "Object does not match target type"

标签 c# string methods reflection invoke

我正在尝试使用字符串调用方法,但出现问题:

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

但是tType,不是那个类型的对象。您需要传入对象以调用那里的方法。 (如果方法是静态的,则为 null)。

关于c# - 在 C# 中使用字符串调用方法时出现 "Object does not match target type",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41970341/

相关文章:

c# - 是否可以将数据从 AngularJS 传递到 Web 服务中带有模型参数的函数?

C# 泛型自引用声明

java - 删除重复的 try/catch 代码

java - 有人可以帮我理解这个 for 循环吗?

java - 如何在 Java 中在一段时间后结束一个方法?

c# - 通用类 : conditional method based on type

string - 使用 Sed 获取第 N 个字符

python - 从 Python 数据框的一列中的每一行中删除前 x 个字符

C 数组循环错误