C# serialize a class without a parameterless constructor
Any Serializer Class need a parameterless constructor because, while deserializing it create an empty new instance, then it copies every public property taken from seialized data.
You can easily make the constructor private, if you want to avoid to create it without parameters.
EX:
public class PgpPublicKey
{
public PgpPublicKey(PublicKeyAlgorithmTag algorithm, AsymmetricKeyParameter pubKey, DateTime time);
private PgpPublicKey();
// cut other methods
}
Yes, the XmlSerializer requires a parameterless constructor to exist in order for the serialization to work.
From the following answer: Why XML-Serializable class need a parameterless constructor
During an object's de-serialization, the class responsible for de-serializing an object creates an instance of the serialized class and then proceeds to populate the serialized fields and properties only after acquiring an instance to populate.
You can make your constructor private or internal if you want, just so long as its parameterless.
DataContractSerializer does not require parameterless constructor. What it instead requires are special attributes of class.