我如何反序列化到一个现有的对象 - C# [英] How do I deserialize into an existing object - C#

查看:150
本文介绍了我如何反序列化到一个现有的对象 - C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C#中,序列化对象到一个文件后,我将如何反序列化文件放回现有对象,而无需创建一个新的对象?

In C#, after serializing an object to a file how would I deserialize the file back into an existing object without creating a new object?

我可以找到自定义序列化所有的例子包括实施一个构造函数,将在反序列化被称为,这正是我想除了我不想要的功能是什么是一个构造函数。

All the examples I can find for custom serialization involve implementing a constructor that will be called upon deserialization which is exactly what I want except that I don't want the function to be a constructor.

谢谢!

推荐答案

有些串行器支持回调;例如,无论是的BinaryFormatter 的DataContractSerializer (和protobuf网,下同)允许您指定前,serializaton回调,并且由于它们的跳过的构造函数,这很可能是足以初始化对象。串行器仍然是的创建的,虽然。

Some serializers support callbacks; for example, both BinaryFormatter and DataContractSerializer (and protobuf-net, below) allow you to specify a before-serializaton callback, and since they skip the constructor, this may well be enough to initialize the object. The serializer is still creating it, though.

大多数串行化器是挑剔想创建新的对象本身,但有些人会允许你反序列化到一个现有的对象。嗯,实际上是跃居脑海中只有一个是protobuf网(披露:我的作者)...

Most serializers are fussy about wanting to create the new object themselves, however some will allow you to deserialize into an existing object. Well, actually the only one that leaps to mind is protobuf-net (disclosure: I'm the author)...

这有2个不同的功能,可能有助于在这里;为的的对象(图即最外层的对象),您可以直接供给现有的对象要么在合并方法(在V1,也目前在V2的兼容性),或(在v2)中的反序列化办法;例如:

This has 2 different features that might help here; for the root object (i.e. the outermost object in a graph) you can supply the existing object directly to either the Merge methods (in v1, also present in v2 for compatibility), or (in v2) the Deserialize methods; for example:

var obj = Serializer.Merge<YourType>(source, instance);

然而,在一个更​​大的图形,你可能想自己提供的其他对象(不仅仅是根) 。以下是没有暴露在的属性的API,但在第2版中的新功能:

However, in a larger graph, you might want to supply other objects yourself (than just the root). The following is not exposed on the attribute API, but is a new feature in v2:

RuntimeTypeModel.Default[typeof(SomeType)].SetFactory(factoryMethod);



其中, factoryMethod 可以是 SOMETYPE A 静态法(即返回 SOMETYPE <中>名称的/ code>实例),或者可以是的MethodInfo 在任何地方任何静态方法。该方法可以附加地(任选)如果希望采取的序列上下文作为参数。那么此方法应该用来提供 SOMETYPE 的所有新实例。

where factoryMethod can be either the name of a static method in SomeType (that returns a SomeType instance), or can be a MethodInfo to any static method anywhere. The method can additionally (optionally) take the serialization-context as a parameter if you want. This method should then be used to supply all new instances of SomeType.

请注意:protobuf网是的的不太一样的BinaryFormatter ;获得最佳效果,你需要告诉它如何将您的会员地图 - 非常类似标记事情 [数据成员] 的WCF / DataContractSerializer的。此可以的是属性,但并不需要是

Note: protobuf-net is not quite the same as BinaryFormatter; for best effect, you need to tell it how to map your members - very similar to marking things as [DataMember] for WCF/DataContractSerializer. This can be attributes, but does not need to be.

这篇关于我如何反序列化到一个现有的对象 - C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆