C# 序列化通用列表归档
我有一个类,其中包含有关图片的信息,例如文件路径、哈希值、字节。 在另一个类中,我得到了一个通用列表,其中放置了保存图片信息的类中的对象。
该类看起来像这样:
[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
序列化此列表的最简单方法是什么?
i got a class which holds info about pictures, like filepath, hashvalue, bytes.
in another class i got a generic list where i put objects from the class that holds picture info.
that class looks like this:
[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);
}
}
my list is just list<picinfo> pi = new list<picinfo>();
what would be the eaziest way to serialize this list?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您想使用
BinaryFormatter
(我真的不建议),您可以使用:如果您想使用
XmlSerializer
(在我看来可能更好),但需要 < code>byte[],然后: 就我个人而言,我会使用 protobuf-net:
Sizes:
BinaryFormatter
: 488 bytesXmlSerializer
: 251 字节If you want to use
BinaryFormatter
(which I really don't advise), you can use:If you want to use
XmlSerializer
(probably preferable IMO), but need thebyte[]
, then:Personally, I'd use protobuf-net:
Sizes:
BinaryFormatter
: 488 bytesXmlSerializer
: 251 bytes