VB.Net Serializers会在公共成员中执行代码吗?

时间:2021-06-15 21:19:52

We wish to use the Binary Formatter. In debugging, thus far, it seems that it does not execute the getters for public properties. Does the XML Serializer behave the same way? Also, during deserialization, will the deserializers use the setters to apply the values during deserialization?

我们希望使用二进制格式化程序。在调试中,到目前为止,似乎它没有执行公共属性的getter。 XML Serializer的行为方式是否相同?此外,在反序列化期间,反序列化器是否会使用setter在反序列化期间应用值?

Thus far, our testing with BinaryFormatter shows that it simply writes directly to and from member variables. It does not step through any of the getters or setters. Is the XML Serializer the same way?

到目前为止,我们使用BinaryFormatter进行测试表明它只是直接写入成员变量和从成员变量中直接写入。它不会逐步通过任何getter或setter。 XML Serializer是一样的吗?

What if a public property did something silly like Random().Next? Will this be serialized by the Binary Formatter? It seems that with the XML Serializer, you would need to decorate this member appropriately to get it to participate. The Binary Formatter seems to only work, again, on member variables.

如果一个公共财产做了像Random()那样愚蠢的事情。接下来呢?这会被Binary Formatter序列化吗?似乎使用XML Serializer,您需要适当地装饰此成员以使其参与。二进制格式化器似乎只适用于成员变量。

Thanks.

3 个解决方案

#1


Think about it this way:

这样考虑一下:

Let's say you're deserializing a class with several serialized properties, and the setter for the last property has side effects that can alter the values of another property. The altered property no longer reflects your serialized data. Do you really want it to use that setter?

假设您正在使用多个序列化属性对类进行反序列化,并且最后一个属性的setter具有可以更改另一个属性的值的副作用。更改的属性不再反映您的序列化数据。你真的想要它使用那个setter吗?

On the other hand, what if there is no backing store for a property? Perhaps it's a composite property allowing you to get and set values of all the others at once. Arguably this property shouldn't be serialized (or only this property should be serialized, depending on how things work), but there could be other examples. How does the formatter know where to assign the value for such a property?

另一方面,如果没有物业的后备存储怎么办?也许它是一个复合属性,允许您同时获取和设置所有其他值。可以说这个属性不应该被序列化(或者只应该序列化这个属性,具体取决于工作方式),但可能还有其他的例子。格式化程序如何知道在何处为此类属性赋值?

So which is it? I had to look it up and couldn't quickly find an authoritative source, but it looks like the XmlSerializer does use getters and setters while the BinaryFormatter does not use getters or setters.

那是哪个呢?我不得不查找它并且无法快速找到权威来源,但看起来XmlSerializer确实使用getter和setter而BinaryFormatter不使用getter或setter。

And that kind of makes sense. My first point showed that you don't really want to use getter/setters. My 2nd point showed that you may have to use them. The binary formatter can just take the exact in-memory representation of the object, so it skips the getter/setters. The XmlSerializer, which doesn't have this ability, has to use the other method.

这是有道理的。我的第一点表明你真的不想使用getter / setter。我的第二点表明你可能不得不使用它们。二进制格式化程序可以只获取对象的精确内存表示,因此它会跳过getter / setter。没有此功能的XmlSerializer必须使用其他方法。

You should probably set up a quick test project for yourself so you can see it in action.

您可能应该为自己设置一个快速测试项目,以便您可以看到它的实际效果。

#2


You need both a getter and a setter or the property will not be serialized. The reason for this is that the serializer assumes it can't set the value so transporting it would be wasteful.

您需要getter和setter,否则属性将不会被序列化。这样做的原因是序列化器假定它无法设置值,因此传输它将是浪费。

You can even have an empty setter and it will work.

你甚至可以有一个空的setter,它会工作。

#3


I just ran a quick test using the XML Serializer. To answer your question: Yes, it does use the getters durning serialization and it does use the setters during deserialization.

我刚刚使用XML Serializer进行了快速测试。回答你的问题:是的,它确实使用durning序列化的getter,它确实在反序列化期间使用setter。

EDIT

Found this in the docs:

在文档中找到了这个:

This example uses a binary formatter to do the serialization. All you need to do is create an instance of the stream and the formatter you intend to use, and then call the Serialize method on the formatter. The stream and the object to serialize are provided as parameters to this call. Although it is not explicitly demonstrated in this example, all member variables of a class will be serialized—even variables marked as private. In this aspect, binary serialization differs from the XMLSerializer Class, which only serializes public fields. For information on excluding member variables from binary serialization, see Selective Serialization.

此示例使用二进制格式化程序进行序列化。您需要做的就是创建流的实例和您打算使用的格式化程序,然后在格式化程序上调用Serialize方法。流和要序列化的对象作为此调用的参数提供。虽然在此示例中未明确说明,但类的所有成员变量都将被序列化 - 甚至标记为私有的变量。在这方面,二进制序列化与XMLSerializer类不同,后者仅序列化公共字段。有关从二进制序列化中排除成员变量的信息,请参阅选择性序列化。

#1


Think about it this way:

这样考虑一下:

Let's say you're deserializing a class with several serialized properties, and the setter for the last property has side effects that can alter the values of another property. The altered property no longer reflects your serialized data. Do you really want it to use that setter?

假设您正在使用多个序列化属性对类进行反序列化,并且最后一个属性的setter具有可以更改另一个属性的值的副作用。更改的属性不再反映您的序列化数据。你真的想要它使用那个setter吗?

On the other hand, what if there is no backing store for a property? Perhaps it's a composite property allowing you to get and set values of all the others at once. Arguably this property shouldn't be serialized (or only this property should be serialized, depending on how things work), but there could be other examples. How does the formatter know where to assign the value for such a property?

另一方面,如果没有物业的后备存储怎么办?也许它是一个复合属性,允许您同时获取和设置所有其他值。可以说这个属性不应该被序列化(或者只应该序列化这个属性,具体取决于工作方式),但可能还有其他的例子。格式化程序如何知道在何处为此类属性赋值?

So which is it? I had to look it up and couldn't quickly find an authoritative source, but it looks like the XmlSerializer does use getters and setters while the BinaryFormatter does not use getters or setters.

那是哪个呢?我不得不查找它并且无法快速找到权威来源,但看起来XmlSerializer确实使用getter和setter而BinaryFormatter不使用getter或setter。

And that kind of makes sense. My first point showed that you don't really want to use getter/setters. My 2nd point showed that you may have to use them. The binary formatter can just take the exact in-memory representation of the object, so it skips the getter/setters. The XmlSerializer, which doesn't have this ability, has to use the other method.

这是有道理的。我的第一点表明你真的不想使用getter / setter。我的第二点表明你可能不得不使用它们。二进制格式化程序可以只获取对象的精确内存表示,因此它会跳过getter / setter。没有此功能的XmlSerializer必须使用其他方法。

You should probably set up a quick test project for yourself so you can see it in action.

您可能应该为自己设置一个快速测试项目,以便您可以看到它的实际效果。

#2


You need both a getter and a setter or the property will not be serialized. The reason for this is that the serializer assumes it can't set the value so transporting it would be wasteful.

您需要getter和setter,否则属性将不会被序列化。这样做的原因是序列化器假定它无法设置值,因此传输它将是浪费。

You can even have an empty setter and it will work.

你甚至可以有一个空的setter,它会工作。

#3


I just ran a quick test using the XML Serializer. To answer your question: Yes, it does use the getters durning serialization and it does use the setters during deserialization.

我刚刚使用XML Serializer进行了快速测试。回答你的问题:是的,它确实使用durning序列化的getter,它确实在反序列化期间使用setter。

EDIT

Found this in the docs:

在文档中找到了这个:

This example uses a binary formatter to do the serialization. All you need to do is create an instance of the stream and the formatter you intend to use, and then call the Serialize method on the formatter. The stream and the object to serialize are provided as parameters to this call. Although it is not explicitly demonstrated in this example, all member variables of a class will be serialized—even variables marked as private. In this aspect, binary serialization differs from the XMLSerializer Class, which only serializes public fields. For information on excluding member variables from binary serialization, see Selective Serialization.

此示例使用二进制格式化程序进行序列化。您需要做的就是创建流的实例和您打算使用的格式化程序,然后在格式化程序上调用Serialize方法。流和要序列化的对象作为此调用的参数提供。虽然在此示例中未明确说明,但类的所有成员变量都将被序列化 - 甚至标记为私有的变量。在这方面,二进制序列化与XMLSerializer类不同,后者仅序列化公共字段。有关从二进制序列化中排除成员变量的信息,请参阅选择性序列化。