C# 序列化通用列表归档

发布于 2024-12-13 05:13:20 字数 1272 浏览 1 评论 0原文

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

该类看起来像这样:

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

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.

that class looks like this:

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

my list is just list<picinfo> pi = new list<picinfo>();
what would be the eaziest way to serialize this list?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

葬心 2024-12-20 05:13:20

如果您想使用 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 (在我看来可能更好),但需要 < code>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();
        }
    }
}

Sizes:

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

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

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

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 bytes
  • XmlSerializer: 251 bytes
  • protobuf-net: 16 bytes
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文