c#序列化泛型列表文件

本文关键字:customObject 文件 列表 序列化 泛型 | 更新日期: 2023-09-27 18:13:19

我得到了一个类,其中包含有关图片的信息,如filepath, hashvalue, bytes。在另一个类中,我得到了一个通用列表,其中我从保存图片信息的类中放置对象。

这个类是这样的:

[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>();序列化这个列表最简单的方法是什么?

c#序列化泛型列表<customObject>文件

如果您想使用BinaryFormatter(我真的不建议),您可以使用:

[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(可能更可取),但需要byte[],然后:

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-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();
        }
    }
}

大小:

  • BinaryFormatter: 488 bytes
  • XmlSerializer: 251 bytes
  • protobuf-net: 16字节