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

问题描述:

执行

我得到这个错误在这行system.invalidoperationexception集合被修改枚举操作可能无法System.Collections.Generic.Dictionary

错误说:

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

线,我有错误在

foreach (ISkill skill in client.Spells.Values) 

当我试图用for更换foreach我有三个错误

UPDATE:我的全码

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

迭代时,不要在'client.Spells'中添加或删除。这也包括来自不同的线程。并且,记得先搜索.. – user2864740

+0

你是什么意思,我不明白你兄弟 –

我在循环里怀疑你正在修改的集合,这是引起例外。

foreach不允许突变你的迭代,用for代替。

+0

当我使用'For'而不是'foreach'我得到错误'只有赋值,调用,增量,减量和新对象表达式可以作为一个语句使用,也可以用作其他两个错误';预计'@Hari Prasad –

+0

除非我看到您的代码,否则难以发表评论,根据我的经验,您的新错误可能是因为将方法和属性互换,这是没有意义的。 –

+0

将我的代码添加到主题@Hari Prasad –