Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
298 views
in Technique[技术] by (71.8m points)

c# - Deserialize part of a binary file

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 MyObject
{
    public string Name { get; set; }

    public int Value { get; set; }

    public IList<MyOtherObject> { get; set; } // lots of data in here (order of kB-MB)
}

What I would like is to be able to deserialize only Name and Value by way of populating a ListView for file selection purposes and then deserialize the rest of the file when needed (i.e. the user chooses that file from the ListView).

As always, any help greatly appreciated and if any 3rd party libraries are suggested they would need to be able to be used freely in a commercial environment.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...