Wib*_*ble 2 c# reflection unity-game-engine
我正在尝试使用Reflection(这对我来说是第一次,我已经针对此错误查看了许多其他答案,但没有找到对我有用的答案)
这是呼叫方法
void OnMouseDown(){
string CardName = "GoldFate";
Type classType = Type.GetType(CardName);
Debug.Log ("Type: " + classType);
MethodInfo theMethod = classType.GetMethod("Resolve"+CardName);
Debug.Log ("MethodInfo: " + theMethod);
theMethod.Invoke(this, null);
}
Run Code Online (Sandbox Code Playgroud)
目标是:
public class GoldFate {
public void ResolveGoldFate(){
Debug.Log ("We got to Gold Fate");
}
}
Run Code Online (Sandbox Code Playgroud)
生成的输出为:
类型:GoldFate
MethodInfo:无效ResolveGoldFate()
TargetException:对象与目标类型不匹配。 System.Reflection.MonoMethod.Invoke(System.Object obj,BindingFlags invokeAttr,System.Reflection.Binder绑定程序,System.Object []参数,System.Globalization.CultureInfo文化)(在/ Users / builduser / buildslave / mono-runtime-和-classlibs / build / mcs / class / corlib / System.Reflection / MonoMethod.cs:236)System.Reflection.MethodBase.Invoke(System.Object obj,System.Object []参数)(位于/ Users / builduser / buildslave) /mono-runtime-and-classlibs/build/mcs/class/corlib/System.Reflection/MethodBase.cs:115)FateCardManager.OnMouseDown()(在Assets / Scripts / Card Manipulation / FateCards / FateCardManager.cs:53)UnityEngine .SendMouseEvents:DoSendMouseEvents(Int32,Int32)
我显然没有收到调试消息
提前致谢
我觉得你的问题就出在这里在这一行:theMethod.Invoke(this, null);
。这里this
需要成为GoldFate
类的实例。一旦确定了这一点,我认为您将能够成功调用该方法。
归档时间: |
|
查看次数: |
5918 次 |
最近记录: |