C#序列化泛型列表< customObject>到文件 [英] C# serialize generic list<customObject> to file
问题描述
我得到了它持有约图片信息,如文件路径,散列值的一类,字节。
在另一个类中我有一个通用的列表,我把对象从保存影像信息类
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.
这类看起来是这样的:
[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);
}
}
我的名单只是列表< ; picinfo> PI =新的列表< picinfo>();
什么是序列化此列表中的eaziest方式
my list is just list<picinfo> pi = new list<picinfo>();
what would be the eaziest way to serialize this list?
推荐答案
如果你想使用的BinaryFormatter
(这我真的不建议),你可以使用:
If you want to use BinaryFormatter
(which I really don't advise), you can use:
[Serializable]
class PicInfo
{
public string fileName { get; set; }
public string completeFileName { get; set; }
public string filePath { get; set; }
public byte[] hashValue { get; set; }
public PicInfo() { }
}
static class Program
{
static void Main()
{
List<PicInfo> pi = new List<PicInfo>();
pi.Add(new PicInfo {fileName = "foo.bar", hashValue = new byte[] {1, 2, 3}});
var ser = new BinaryFormatter();
using (var ms = new MemoryStream())
{
ser.Serialize(ms, pi);
var bytes = ms.ToArray();
}
}
}
如果你想使用的XmlSerializer
(可能是最好IMO),但需要字节[]
,则:
If you want to use XmlSerializer
(probably preferable IMO), but need the byte[]
, then:
public class PicInfo
{
public string fileName { get; set; }
public string completeFileName { get; set; }
public string filePath { get; set; }
public byte[] hashValue { get; set; }
public PicInfo() { }
}
static class Program
{
static void Main()
{
List<PicInfo> pi = new List<PicInfo>();
pi.Add(new PicInfo {fileName = "foo.bar", hashValue = new byte[] {1, 2, 3}});
var ser = new XmlSerializer(typeof(List<PicInfo>));
using (var ms = new MemoryStream())
{
ser.Serialize(ms, pi);
var bytes = ms.ToArray();
}
}
}
就个人而言,我会使用protobuf网:
Personally, I'd use protobuf-net:
[ProtoContract]
public class PicInfo
{
[ProtoMember(1)]public string fileName { get; set; }
[ProtoMember(2)]public string completeFileName { get; set; }
[ProtoMember(3)]public string filePath { get; set; }
[ProtoMember(4)]public byte[] hashValue { get; set; }
public PicInfo() { }
}
static class Program
{
static void Main()
{
List<PicInfo> pi = new List<PicInfo>();
pi.Add(new PicInfo {fileName = "foo.bar", hashValue = new byte[] {1, 2, 3}});
using (var ms = new MemoryStream())
{
Serializer.Serialize(ms, pi);
var bytes = ms.ToArray();
}
}
}
尺寸:
Sizes:
-
的BinaryFormatter
:488字节 -
的XmlSerializer
:251字节 - protobuf网:16字节
BinaryFormatter
: 488 bytesXmlSerializer
: 251 bytes- protobuf-net: 16 bytes
这篇关于C#序列化泛型列表< customObject>到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!