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