system.invalidoperationexception集合被修改枚举操作可能无法在System.Collections.Generic.Dictionary执行

时间:2016-08-23 03:56:37

标签: c# enumeration

我在这一行中收到此错误

错误说:

  

system.invalidoperationexception集合被修改枚举操作可能无法在System.Collections.Generic.Dictionary执行'2.ValueCollection.Enumerator.MoveNext<>

我在

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

当我尝试将foreach替换为for时,我遇到了三个错误

更新:我的完整代码

 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();
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

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

foreach不允许对您迭代的内容进行突变,而是使用for