簡體   English   中英

C#序列化通用列表 <customObject> 提交

[英]C# serialize generic list<customObject> to file

我有一個類,其中包含有關圖片的信息,如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>(); 序列化這個列表最簡單的方法是什么?

如果你想使用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 (可能更喜歡IMO),但需要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個字節
  • XmlSerializer :251個字節
  • protobuf-net:16個字節

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM