When the object sequences to XML, it means that the class of the object must have a parameter constructor. If there is no parameter constructor, the serialization process will encounter abnormalities, as shown in the following errors:
"CSHARPCONSOLE.FOO cannot be serialized because it has no parameter constructor."
Why do I need a constructor?
In the process of deepery, the XML serialization program creates an instance of the serialized class before filling its field and attributes. This instance is essential for serialization to allocate the value to the counter -sequentialized object. If there is no parameter constructor, the serialization program will not be able to create necessary instances. Therefore, it cannot be reverse -serialized, which leads to the above abnormalities.
The accessability of the constructor
Although there must be no constructor function, it does not necessarily be open accessible. Internal or private non -constructor functions are sufficient for XML serialization.
Conclusion
Knowing the requirements of non -constructive functions are essential for successful XML serialization. Keep in mind that as long as the constructor meets the basic purpose created by the creation of the instance of the device, it can be private or inside.
The above is the detailed content of Why Does XML Serialization Require a Parameterless Constructor?. For more information, please follow other related articles on the PHP Chinese website!