Design without default constructor

不羁的心 提交于 2019-11-30 20:20:27

Create a private default constructor

private DerivedClass()
{
    // code
}

The serialzer will successfully call this even though it's private

The class deserializing your instances requires a parameterless constructor to create the instance, but you don't have to implement a public constructor -- it's enough to have a private or internal constructor as long as it needs no parameters.

By the way, you can as well use the DataContractSerializer, which does not require a parameterless constructor and creates XML, too; it's always my primary choice :-)

  • Have you tried to create a private parameterless constructor?

  • If there is the need of a public one, you can always comment saying that it should not be used (not the best solution)

You also need to create properties in your Serializable class. Variables are not considered and will not be read or written during serialization/deserealization process

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