“对象与目标类型不匹配".在C#中使用字符串调用方法时 [英] "Object does not match target type" when calling methods using string in C#

查看:1181
本文介绍了“对象与目标类型不匹配".在C#中使用字符串调用方法时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm trying to call a method using a string, but there a problem:

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
}

总是存在与最后一行有关的错误对象与目标类型不匹配".你有什么建议?

There's always this error "Object does not match target type" related with the last line. Do you have any recommendations?

推荐答案

method.Invoke(t,new object[] { mov }));

与通话相同

t.WhateverTheMethodIs(mov);

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

But t is the Type, not the object of that type. You need to pass in the object to call the method on there instead. (Or null if the method is static).

这篇关于“对象与目标类型不匹配".在C#中使用字符串调用方法时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆