C#将通用列表<customObject>序列化为文件

Yus*_*tme 7 c# serialization generic-list

我有一个类,其中包含有关图片的信息,如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);
        }
    }
Run Code Online (Sandbox Code Playgroud)

我的列表是list<picinfo> pi = new list<picinfo>(); 序列化这个列表最简单的方法是什么?

Mar*_*ell 13

如果你想使用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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

如果你想使用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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

就个人而言,我使用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();
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

尺寸:

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

  • @Yustme很少有序列化器会写"只是一个txt文件" - 它们将被格式化; 也许是定制的二进制文件,也许是xml,也许是json - 但从不"只是一个txt文件"(除非你把xml或json算作"只是文本").Re"BinaryFormatter":很多原因; 它有一个版本不容忍的习惯,它不是跨平台的(例如,即使在Silverlight上也没用),它很慢,而且它有超重输出.即使是无形的变化,比如制作一个自动实现的属性也会破坏它.哦,它往往会通过事件吮吸不需要的对象. (2认同)