系统.枚举操作可能无法在System.Collections.Generic.Dictionary中执行

本文关键字:Collections System Generic Dictionary 执行 操作 枚举 系统 | 更新日期: 2023-09-27 18:03:35

我在这一行得到这个错误

错误说:

系统。在System.Collections.Generic.Dictionary '2.ValueCollection.Enumerator.MoveNext<>

错误行
foreach (ISkill skill in client.Spells.Values)

当我试图用for替换foreach时,我得到了三个错误

更新:我的完整代码

 public static void SaveSpells(GameState client)
    {
        if (client.Fake) return;
        if (client.TransferedPlayer) return;
        if (((client.Entity != null) && (client.Spells != null)) && (client.Spells.Count != 0))
        {

            foreach (ISkill skill in client.Spells.Values)
            {
                DeadPool.Database.MySqlCommand command;
                if (skill.Available)
                {
                  //  if (skill.PreviousLevel != skill.Level)
                    {
                        command = new DeadPool.Database.MySqlCommand(MySqlCommandType.UPDATE);
                        command.Update("skills").Set("LevelHu2", skill.LevelHu2).Set("LevelHu", (long)skill.Souls).Set("Level", (long)skill.Level).Set("PreviousLevel", (long)skill.Level).Set("PreviousLevel", (long)skill.PreviousLevel).Set("Experience", (long)skill.Experience).Where("EntityID", (long)client.Entity.UID).And("ID", (long)skill.ID).Execute();
                    }
                }
                else
                {
                    skill.Available = true;
                    command = new DeadPool.Database.MySqlCommand(MySqlCommandType.INSERT);
                    command.Insert("skills").Set("LevelHu2", skill.LevelHu2).Set("LevelHu2", skill.LevelHu2).Insert("LevelHu", (long)skill.Souls).Insert("Level", (long)skill.Level).Insert("PreviousLevel", (long)skill.Level).Insert("Experience", (long)skill.Experience).Insert("EntityID", (long)client.Entity.UID).Insert("ID", (long)skill.ID).Execute();
                }
            }
        }
    }

系统.枚举操作可能无法在System.Collections.Generic.Dictionary中执行

我怀疑你在循环中修改了集合,这会导致异常。

foreach不允许对你迭代的东西进行突变,而是使用for

相关文章: