【发布时间】:2011-11-01 08:56:39
【问题描述】:
我有一个类,它包含有关图片的信息,例如文件路径、哈希值、字节。 在另一个类中,我得到了一个通用列表,我在其中放置了包含图片信息的类中的对象。
那个类看起来像这样:
[Serializable()]
class PicInfo : ISerializable
{
public string fileName { get; set; }
public string completeFileName { get; set; }
public string filePath { get; set; }
public byte[] hashValue { get; set; }
public PicInfo()
{ }
public PicInfo(SerializationInfo info, StreamingContext ctxt)
{
this.fileName = (string)info.GetValue("fileName", typeof(string));
this.completeFileName = (string)info.GetValue("completeFileName", typeof(string));
this.filePath = (string)info.GetValue("filePath", typeof(string));
this.hashValue = (byte[])info.GetValue("hashValue", typeof(byte[]));
}
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("fileName", this.fileName);
info.AddValue("completeFileName", this.completeFileName);
info.AddValue("filePath", this.filePath);
info.AddValue("hashValue", this.hashValue);
}
}
我的名单只是list<picinfo> pi = new list<picinfo>();
序列化此列表的最简单方法是什么?
【问题讨论】:
-
您真的应该再澄清一下您的问题。您在寻找什么序列化格式?人类可读?二进制?请提供更多信息
-
不使用 ISerializable 也能正常工作。 .NET 可以将它们全部序列化以方便使用..?
-
格式应该是二进制的,很抱歉没有告诉这个。
-
@mtijn 不正确;仅适用于
XmlSerializer -
@Yustme 如果你使用
BinaryFormatter(我假设你是),它将是二进制的无论你是否实现ISerializable。我强烈建议不要实施ISerializable,除非你有充分的理由(但是:我也强烈建议首先不要使用BinaryFormatter;p)
标签: c# serialization generic-list