本文介绍了反序列化的二进制文件的一部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 限时删除!! 是否有可能反序列化的二进制文件的一部分? 基本上我有类似下面我序列化为二进制文件的对象。 公共类MyObject的 {公共字符串名称{;组; } 公共int值{搞定;组; } 阴毛的IList< MyOtherObject> {搞定;组; } //大量的数据在她(KB-MB的顺序)} 什么我希望它能够反序列化仅名称和值通过填充的方式的ListView 为文件选择的目的,然后在需要的时候反序列化文件的剩余部分(即用户选择从该文件中的的ListView 。 一如往常,任何帮助,不胜感激,如果任何第三方库的建议,他们将需要能够将在商业环境中放心使用。 解决方案 protobuf网可以做到这一点,因为它不依赖于特定的类型,例如: 使用protobuf的; :使用System.IO; [ProtoContract] 公共类使用System.Collections.Generic MyOtherObject {} [ProtoContract] 公共类MyObject的 { [ProtoMember(1)] 公共字符串名称{;设置;} [ ProtoMember(2)] 公共int值{搞定;组; } [ProtoMember(3)] 公开的IList&所述; MyOtherObject>项目{搞定;组; } } [ProtoContract] 公共类MyObjectLite { [ProtoMember(1)] 公共字符串名称{;组; } [ProtoMember(2)] 公共int值{搞定;组; } } 静态类节目 {静态无效的主要() { VAR OBJ =新MyObject的 { NAME =ABC,值= 123,项=新的List< MyOtherObject> {新MyOtherObject(),新MyOtherObject(),新MyOtherObject(),新MyOtherObject(),} }; 使用(var文件= File.Create(foo.bin)) { Serializer.Serialize(文件,OBJ); } MyObjectLite精简版; 使用(var文件= File.OpenRead(foo.bin)) { =精简版 - Serializer.Deserialize LT; MyObjectLite>(文件); } } } 但是,如果你不这样做想要两个不同的类型,和/或你不希望有添加属性 - 可以做得太:使用 ProtoBuf.Meta; 使用System.Collections.Generic; :使用System.IO; 公共类MyOtherObject {} 公共类MyObject的 {公共字符串名称{;组; } 公共int值{搞定;组; } 公众的IList< MyOtherObject>项目{搞定;组; } } 静态类节目 {静态只读RuntimeTypeModel fatModel,liteModel; 静态程序() { //配置机型 fatModel = TypeModel.Create(); fatModel.Add(typeof运算(MyOtherObject),FALSE); fatModel.Add(typeof运算(myObject的),假的)。新增(姓名,价值,项目); liteModel = TypeModel.Create(); liteModel.Add(typeof运算(MyOtherObject),FALSE); liteModel.Add(typeof运算(myObject的),假的)。新增(姓名,值); } 静态无效的主要() { VAR OBJ =新MyObject的 { NAME =ABC,值= 123 ,项=新的List< MyOtherObject> {新MyOtherObject(),新MyOtherObject(),新MyOtherObject(),新MyOtherObject(),} }; 使用(var文件= File.Create(foo.bin)) { fatModel.Serialize(文件,OBJ); } MyObject的精简版; 使用(var文件= File.OpenRead(foo.bin)) { =精简版(为MyObject)liteModel.Deserialize(档,空的typeof(MyObject来) ); } } } 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; } pubic IList<MyOtherObject> { get; set; } //lots of data in her (order of kB-MB)}What I would like it 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. 解决方案 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)); } }} 这篇关于反序列化的二进制文件的一部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 1403页,肝出来的.. 09-09 01:26