I have an object graph containing nodes of various types. All nodes derive from a Node class. Using YamlDotNet I've managed to serialize and deserialize the graph by providing my own implementation of IObjectFactory
. The only thing left to do is to get rid of a constructor that only exists to please the YamlDotNet serializer.
Have a look at the following .NET fiddle https://dotnetfiddle.net/KJMzxD
The FancyNode.ctor()
is the constructor I would like to remove but I'm not sure how to tell the serializer that I've handled everything in the deserializer. If I simply remove it I get the following error
Type 'FancyNode' cannot be deserialized because it does not have a default constructor or a type converter.
If you only want to get rid of the parameterless constructor code rather than the constructor itself - given it's required for that type of deserialisation - you could remove both constructors and use a factory method to create the nodes. That would result in the class having a default public constructor.
For example, change:
to:
Yes you lose the tight control you have that doesn't allow the object to be created without passing those parameters in, given that anyone can now do
var x = new FancyNode()
. Then again you aren't validating the parameters so it makes no difference to calling it withnew FancyNode(null, null)
.