小编Wib*_*ble的帖子

对象与使用反射的单位中的目标类型不匹配

我正在尝试使用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 …

c# reflection unity-game-engine

2
推荐指数
1
解决办法
5918
查看次数

创建字典<string,class>

在使用Unity的C#中,我正在尝试创建一个类的字典.

所有类都继承自具有许多虚方法的基类(构建脚本)

我已经设置了我的词典:

Dictionary<string, BuildingScript> buildingScripts = new Dictionary<string, BuildingScript>();
Run Code Online (Sandbox Code Playgroud)

我正试图添加到词典中:

buildingScripts.Add("Farm", FarmScript);
buildingScripts.Add("Hunter", HunterScript);
buildingScripts.Add("Fisher", FisherScript);
Run Code Online (Sandbox Code Playgroud)

编译器正在重新认识继承,所以我想弄清楚我错过了什么.

错误消息是:

  • 错误CS0119:表达式表示type', where a变量', value' or方法组'是预期的
  • 'System.Collections.Generic.Dictionary.Add(string,BuildingScript)'的最佳重载方法匹配有一些无效的参数
  • 参数#2' cannot convert对象'表达式以输入'BuildingScript'

.net c# dictionary

2
推荐指数
1
解决办法
1620
查看次数

标签 统计

c# ×2

.net ×1

dictionary ×1

reflection ×1

unity-game-engine ×1