如何从foreach调用的方法中获得收益

时间:2019-10-15 15:03:34

标签: c# unity3d

我有一个枚举,包含资源ID,称为Resources.Id。

另一个脚本Resources.cs具有根据提供的枚举生成特定字符串的方法。我想遍历所有枚举,在字典中存储一些与它们有关的字符串。

代码运行良好,直到QtyAddress中的最后一个返回调用为止。我做了一些调试,返回后立即丢失了堆栈跟踪。

PlayerData.cs

Dictionary<string, string> list = new Dictionary<string, string>();

        // Resources
foreach (Resources.Id resource in Enum.GetValues(typeof(Resources.Id))) 
{
    string key = Resources.QtyAddress(resource);
    list.Add(key , "0");
}

Resources.cs

public static string NamePlural(Id id) 
{
    string String;

    switch (id) 
    {
        case (Resources.Id.Technology):
          String = "Technology";
          break;

        default:
          String = "NONE";
          break;    
    }

    return String;
}

public static string QtyAddress(Id id) 
{
    string String;
    switch (id) 
    {
        case (Resources.Id.Technology):
          String = string.Format("{0}Qty", NamePlural(id));
          break;

        default:
          String = "NONE";
          break;
    }
    return String;
}

控制台或堆栈跟踪中没有错误消息。

该调试通过NamePlural()中的返回调用,并在QtyAddress()返回之前停止。所有字符串均已根据调试器正确设置。

1 个答案:

答案 0 :(得分:0)

找到了解决方案!!!

其中一个地址有一个小错字。但是不知何故,编译器没有抛出错误,而是无限等待,这使我们无须通知。

基本上,它试图添加重复的索引键,因为构建器不可能显示该错误,但是未显示。

谢谢大家!

相关问题