Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/csharp-4.0/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 4.0 尝试枚举反序列化列表时引发异常_C# 4.0_Exception_Protobuf Net - Fatal编程技术网

C# 4.0 尝试枚举反序列化列表时引发异常

C# 4.0 尝试枚举反序列化列表时引发异常,c#-4.0,exception,protobuf-net,C# 4.0,Exception,Protobuf Net,我知道这个例外已经被解决了几十亿次,但我的情况略有不同(我想) 无论如何,我使用ProtoBuf-Net来保存和加载对象。我有一个我正试图反序列化的对象列表,但它一直在说(这里有): 再一次,我在这里看到这个问题被问了50次,所以我很抱歉50次,但代码如下: public void Load(){ using (var file = File.Exists(Application.StartupPath + @"\TestFile.scn

我知道这个例外已经被解决了几十亿次,但我的情况略有不同(我想)

无论如何,我使用ProtoBuf-Net来保存和加载对象。我有一个我正试图反序列化的对象列表,但它一直在说(这里有):

再一次,我在这里看到这个问题被问了50次,所以我很抱歉50次,但代码如下:

public void Load(){
            using (var file = 
                File.Exists(Application.StartupPath + @"\TestFile.scn") ? 
                File.OpenRead("TestFile.scn") : 
                null){
                if (file != null){
                    this._tlpGrid.Controls.Clear();
                    this.Scenes = Serializer.Deserialize<List<GraphicsPanel>>(file);
                    foreach(GraphicsPanel gp in this._lgpScenes)
                        this.AddScene(gp);
                }
            }
}
修改:

    public void AddScene(GraphicsPanel Scene){
        this._tlpGrid.Controls.Add(Scene);
        if (!this.Scenes.Contains(Scene))
            this.Scenes.Add(Scene);
    }

这个问题已经得到了回答,非常感谢。

因此,很明显,问题在于我调用的一个方法修改了列表,而它正在迭代列表。这本应该是显而易见的,但我完全错过了。谢谢你给我指出来,李。 引发异常的方法的代码:

public void Load(){
            using (var file = 
                File.Exists(Application.StartupPath + @"\TestFile.scn") ? 
                File.OpenRead("TestFile.scn") : 
                null){
                if (file != null){
                    this._tlpGrid.Controls.Clear();
                    this.Scenes = Serializer.Deserialize<List<GraphicsPanel>>(file);
                    foreach(GraphicsPanel gp in this._lgpScenes)
                        this.AddScene(gp);
                }
            }
}
及之后:

public void AddScene(GraphicsPanel Scene){
    this._tlpGrid.Controls.Add(Scene);
    if (!this.Scenes.Contains(Scene))
        this.Scenes.Add(Scene);
}

再次感谢您耐心地指出了显而易见的问题。

因此,很明显,问题在于我调用的一个修改列表的方法在迭代列表时修改了列表。这本应该是显而易见的,但我完全错过了。谢谢你给我指出来,李。 引发异常的方法的代码:

public void Load(){
            using (var file = 
                File.Exists(Application.StartupPath + @"\TestFile.scn") ? 
                File.OpenRead("TestFile.scn") : 
                null){
                if (file != null){
                    this._tlpGrid.Controls.Clear();
                    this.Scenes = Serializer.Deserialize<List<GraphicsPanel>>(file);
                    foreach(GraphicsPanel gp in this._lgpScenes)
                        this.AddScene(gp);
                }
            }
}
及之后:

public void AddScene(GraphicsPanel Scene){
    this._tlpGrid.Controls.Add(Scene);
    if (!this.Scenes.Contains(Scene))
        this.Scenes.Add(Scene);
}

再次感谢您耐心地指出显而易见的问题。

AddScene()有什么作用?我假设
AddScene
正在修改
\u lgpScenes
,而您不能在上面枚举。aaaaahg您是对的!!!编辑代码以反映问题。如果问题已得到回答,@lee请在下面写下答案,以便将其标记为已接受的答案。AddScene()做什么?我假设
AddScene
正在修改
\lgpScenes
,而您在上面枚举时不能这样做。aaaaahg您是对的!!!编辑代码以反映问题。如果问题已回答,@lee请在下面填写答案,以便将其标记为已接受的答案。
public void AddScene(GraphicsPanel Scene){
    this._tlpGrid.Controls.Add(Scene);
    if (!this.Scenes.Contains(Scene))
        this.Scenes.Add(Scene);
}