Deserialize part of a binary file

前端 未结 2 876
被撕碎了的回忆
被撕碎了的回忆 2020-12-17 01:28

Is it possible to deserialize part of a binary file?

Basically I have an object similar to below, which I serialize into a binary file.

public class          


        
相关标签:
2条回答
  • 2020-12-17 01:57

    How about putting the Name and Valueinto a superclass and serializing them separately?

    Alternatively, you could maintain a Dictionary and serialize that into one file.

    0 讨论(0)
  • 2020-12-17 02:03

    protobuf-net can do that, because it is not tied to the specific type; for example:

    using ProtoBuf;
    using System.Collections.Generic;
    using System.IO;
    
    [ProtoContract]
    public class MyOtherObject { }
    [ProtoContract]
    public class MyObject
    {
        [ProtoMember(1)]
        public string Name { get; set; }
        [ProtoMember(2)]
        public int Value { get; set; }
        [ProtoMember(3)]
        public IList<MyOtherObject> Items { get; set; }
    }
    
    [ProtoContract]
    public class MyObjectLite
    {
        [ProtoMember(1)]
        public string Name { get; set; }
        [ProtoMember(2)]
        public int Value { get; set; }
    }
    
    static class Program
    {
        static void Main()
        {
            var obj = new MyObject
            {
                Name = "abc",
                Value = 123,
                Items = new List<MyOtherObject>
                {
                    new MyOtherObject(),
                    new MyOtherObject(),
                    new MyOtherObject(),
                    new MyOtherObject(),
                }
            };
            using (var file = File.Create("foo.bin"))
            {
                Serializer.Serialize(file, obj);
            }
            MyObjectLite lite;
            using (var file = File.OpenRead("foo.bin"))
            {
                lite= Serializer.Deserialize<MyObjectLite>(file);
            }
        }
    }
    

    But if you don't want two different types, and/or you don't want to have to add attributes - that can be done too:

    using ProtoBuf.Meta;
    using System.Collections.Generic;
    using System.IO;
    
    public class MyOtherObject { }
    public class MyObject
    {
        public string Name { get; set; }
        public int Value { get; set; }
        public IList<MyOtherObject> Items { get; set; }
    }
    static class Program
    {
        static readonly RuntimeTypeModel fatModel, liteModel;
        static Program()
        {
            // configure models
            fatModel = TypeModel.Create();
            fatModel.Add(typeof(MyOtherObject), false);
            fatModel.Add(typeof(MyObject), false).Add("Name", "Value", "Items");
            liteModel = TypeModel.Create();
            liteModel.Add(typeof(MyOtherObject), false);
            liteModel.Add(typeof(MyObject), false).Add("Name", "Value");
        }
        static void Main()
        {
            var obj = new MyObject
            {
                Name = "abc",
                Value = 123,
                Items = new List<MyOtherObject>
                {
                    new MyOtherObject(),
                    new MyOtherObject(),
                    new MyOtherObject(),
                    new MyOtherObject(),
                }
            };
            using (var file = File.Create("foo.bin"))
            {
                fatModel.Serialize(file, obj);
            }
            MyObject lite;
            using (var file = File.OpenRead("foo.bin"))
            {
                lite = (MyObject)liteModel.Deserialize(
                    file, null, typeof(MyObject));
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题