rializationInfo中没有TryGetValue方法呢

rializationInfo中没有TryGetValue方法呢

本文介绍了为什么SerializationInfo中没有TryGetValue方法呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在实现C#中的 ISerializable的接口,我们提供了一个构造函数,它接受一个的SerializationInfo 对象,然后查询它与各种 GetInt32等 GetObject的等,以填充对象的字段的方法,我们正在尝试反序列化。

When implementing the ISerializable interface in C#, we provide a constructor which takes a SerializationInfo object, and then queries it with various GetInt32, GetObject etc. methods in order to fill the fields of the object which we are trying to deserialize.

要实现这个接口,而不是仅仅使用 [Serializable接口] 属性的一个重要原因,是为了向下兼容:如果我们在某个时候添加新字段的类,我们可以赶上 SerializationException 通过系列化,旧版本的类抛出,并以适当的方式处理。

One major reason to implement this interface, rather than just using the [Serializable] attribute, is for backwards compatibility: if we have added new fields to the class at some point, we can catch the SerializationException thrown by a serialized, older version of the class, and handle them in an appropriate manner.

我的问题是:我们为什么要使用这些例外的是,本质上,控制流?如果我反序列化大量其中被保存前一段时间,有可能每个领域的缺失每班会抛出异常,从而导致非常糟糕的性能等级。

My question is the following: why do we have to use these exceptions for what is, essentially, control flow? If I am deserializing a large number of classes which were saved some time ago, potentially each missing field in each class will throw an exception, causing really bad performance.

为什么的SerializationInfo 类没有提供 TryGetValue 方法这将简单地返回如果名称字符串不在场?

Why does the SerializationInfo class not provide TryGetValue methods which would simply return false if the name string were not present?

推荐答案

您可以遍历可用字段,并使用开关,虽然...

You can iterate over the available fields and use switch, though...

            foreach(SerializationEntry entry in info) {
                switch(entry.Name) {
                    ...
                }
            }

或者你可以使用protobuf网;-p

Or you could use protobuf-net ;-p

这篇关于为什么SerializationInfo中没有TryGetValue方法呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-20 16:22