Destroy part of a binary file

Is it possible to deserialize part of a binary file?

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

public class MyObject { public string Name { get; set; } public int Value { get; set; } pubic IList<MyOtherObject> { get; set; } //lots of data in her (order of kB-MB) } 

What I would like for it to be able to deserialize only Name and Value by populating the ListView for file selection purposes and then deserialize the rest of the file as needed (i.e. the user selects this file from the ListView .

As always, any help is greatly appreciated, and if any third-party libraries are offered, they should be free to use in a commercial environment.

+6
source share
2 answers

protobuf-net can do this because it is not bound to a specific type; eg:

 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 do not need two different types and / or you do not want to add attributes - this can also be done:

 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)); } } } 
+6
source

How to add Name and Value to a superclass and serialize them separately?

Alternatively, you can maintain the dictionary and serialize it into a single file.

0
source

Source: https://habr.com/ru/post/918932/


All Articles