Why do you have to mark a class with the attribute [serializable]?

痞子三分冷 提交于 2019-11-27 03:20:07

问题


Seeing as you can convert any document to a byte array and save it to disk, and then rebuild the file to its original form (as long as you have meta data for its filename etc.).

Why do you have to mark a class with [Serializable] etc? Is that just the same idea, "meta data" type information so when you cast the object to its class things are mapped properly?


回答1:


First off, you don't have to.

It is simply a marker interface that tells the serializer that the class is composed of items that it can serialize (which may or may not be true) and that is can use the default serialization.

The XMLSerializer has the additional requirement to have a zero parameter constructor to the class.

There are other serializers that use contracts for serialization (such as the DataContractSerializer) - they give you more control over serialization than simply marking a class as Serializable. You can also get more control by implementing the ISerializable interface.




回答2:


Binary serialization is pretty powerful, it can create an instance of a class without running the constructor and can set fields in your class that you declared private. Regular code can of course not do this. By applying the [Serializable] attribute, you explicitly give it the go-ahead to mess with your private parts. And you implicitly give that permission to only the BinaryFormatter class.

XML serialization doesn't need this kind of okay, it only serializes members that are public.

DataContractSerializer can serialize private members as well. It therefore needs an explicit okay again, now with the [DataContract] attribute.




回答3:


It's basically metadata that indicates that a class can be serialized, nothing more.

It is required by a lot of framework serializers, which refuse to deal with types not having this attribute applied to them.




回答4:


Serialization can create security holes and may be plagued by versioning problems. On top of that, for some classes, the very idea of serialization is outright nonsense.

For details, see the excellent answers to Why Java needs Serializable interface?, especially this one, this one, and this one. They make the case that serialization should be a feature you have to explicitly opt into.

For a counterpoint, the accepted answer to that question makes the case that classes should be serializable by default.




回答5:


It indicates to the serializer that you want that class to be serialized as you may not want all properties or classes to be serialized.




回答6:


I see it as a reminder that I will allow the class to be serialized. So you don't implicitly serialize something you shouldn't.

Don't know it that is designers' intention.

BTW, I just love BinaryFormatter and use it as much as I can. It handles pretty much of the stuff automatically (like rebuilding complex object graphs with recurring references spread throughout the graph).



来源:https://stackoverflow.com/questions/2595104/why-do-you-have-to-mark-a-class-with-the-attribute-serializable

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!