gpt4 book ai didi

C# 序列化泛型列表 到文件

转载 作者:太空狗 更新时间:2023-10-29 20:03:30 24 4
gpt4 key购买 nike

我得到了一个包含图片信息的类,比如文件路径、哈希值、字节。在另一个类中,我得到了一个通用列表,我在其中放置了包含图片信息的类中的对象。

那个类看起来像这样:

[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 字节

关于C# 序列化泛型列表<customObject> 到文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7964280/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com