Should any domain object, not be serializable?

情到浓时终转凉″ 提交于 2019-12-23 16:45:51

问题


Is there a way to just tell the compiler, that I want my objects to be serializable by default?


回答1:


Pretty much every serialization engine is going to want to know that your objects are suitable. This can take the form of:

  • [Serializable]/ISerializable (BinaryFormatter,SoapFormatter)
  • [Serializable]/IXmlSerializable (and public) (XmlSerializer)
  • [DataContract]/[MessageContract] (or most of the above) (DataContractSerializer and variants)

AFAIK, there is no way of avoiding this step.




回答2:


No, you need to decorate the objects with the Serializable attribute. FYI all objects are Xml Serializable by default. The Xml Serializer doesn't require the attribute.

I'm not sure about DataContracts but last I checked you needed to decorate them as well.



来源:https://stackoverflow.com/questions/340657/should-any-domain-object-not-be-serializable

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