Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.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# 列表中只有第一个对象被反序列化_C#_Serialization_Binaryformatter - Fatal编程技术网

C# 列表中只有第一个对象被反序列化

C# 列表中只有第一个对象被反序列化,c#,serialization,binaryformatter,C#,Serialization,Binaryformatter,我在反序列化对象列表时遇到问题。由于某些原因,load方法只返回序列化列表中的第一个对象。我已经研究了许多似乎相关的问题,但没有找到解决我具体问题的方法。我想这可能与我使用的私人支持收藏有关?任何帮助都将不胜感激。(已将类名和方法设置为通用) 以下是我的反序列化方法: public static ObjectList LoadLibrary() { BinaryFormatter formatter = new BinaryFormatter();

我在反序列化对象列表时遇到问题。由于某些原因,load方法只返回序列化列表中的第一个对象。我已经研究了许多似乎相关的问题,但没有找到解决我具体问题的方法。我想这可能与我使用的私人支持收藏有关?任何帮助都将不胜感激。(已将类名和方法设置为通用)

以下是我的反序列化方法:

    public static ObjectList LoadLibrary()
    {
        BinaryFormatter formatter = new BinaryFormatter();
        FileStream loadStream;
        string path = System.IO.Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "ObjectList.bin");
        ObjectList lib = null;

        if (!File.Exists(path))
        {
            lib = new ObjectList();
            return lib;
        }

        try
        {
            using (loadStream = new FileStream(path, FileMode.Open, FileAccess.Read))
            {
                lib = (ObjectList)formatter.Deserialize(loadStream);
            }
        }
        catch (Exception e)
        {
            MessageBox.Show("Object list could not be loaded. Error: \n" + e);
        }
        return lib;
    }
这是我的ObjectList类

[Serializable]
public class ObjectList: IEnumerable<Obj>
{
    #region Construction
    /// <summary>
    /// Contracts Test Case Library Constructor
    /// </summary>
    public ObjectList() { }

    /// <summary>
    /// Private backing collection
    /// </summary>
    private List<Obj> _objectList = new List<Obj>();
    #endregion

没有关系。。。我意识到这是我的保存方法的问题。我在使用filemode append时不太合适。别担心。。。我意识到这是我的保存方法的问题。我使用的是filemode append,但这并不合适。
[Serializable]
public class Obj
{
    #region Constructors
    public Obj(string Name, string Description)
    {
        this.Name = Name;
        this.Description = Description;
    }

    public Obj() { }
    #endregion