C# system.invalidoperationexception集合已修改枚举操作不能在system.Collections.Generic.Dictionary上执行

C# system.invalidoperationexception集合已修改枚举操作不能在system.Collections.Generic.Dictionary上执行,c#,enumeration,C#,Enumeration,我在这行中遇到了这个错误 错误是: system.invalidoperationexception集合已修改枚举操作不能在system.Collections.Generic.Dictionary'2.ValueCollection.Enumerator.MoveNext上执行 行中有错误 foreach (ISkill skill in client.Spells.Values) 当我试图用for替换foreach时,出现了三个错误 更新:我的完整代码 public static voi

我在这行中遇到了这个错误

错误是:

system.invalidoperationexception集合已修改枚举操作不能在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();
                }
            }
        }
    }

我怀疑在循环内部您正在修改集合,这导致了异常


foreach
不允许对您迭代的内容进行突变,请改用
for

迭代时不要向
客户端添加或从中删除拼写。这还包括来自不同线程的消息。还有,记住先搜索..你是什么意思,我不明白你的意思当我用'For'代替'foreach'时,我得到了错误';预期为“@Hari prasad(在将其更改为for循环后),您是否可以使用更新的代码进行更新?并突出显示您从哪里得到错误?