4

I have a requirement where I need to Deserialize and Create few objects of a class(Table) which doesn't have a Default Constructor.

Snippet of my code

else if (reader.Name == "Tables")
{
    reader.ReadStartElement();
    tables = SerializationHelper<Table>.DeserializeList(reader);
}

The definition of DeserializeList in SerializationHelper is as below:

public static List<T> DeserializeList(XmlReader reader)
{
    XmlSerializer ser = new XmlSerializer(typeof(T));
    List<T> returnval = new List<T>();
    while (reader.NodeType != XmlNodeType.EndElement)
    {
        T result = (T)ser.Deserialize(reader);
        returnval.Add(result);
    }
    return returnval;
}

This is an existing working code and with recent changes we had to add a mandate parameter to all the Constructors in the Class

The Table class here doesn't have any Parameter-less Constructor now.

I wanted to find out if I can by anyway pass-on at-least one parameter when DeSerializing the Table objects.

I have already Read the following but they use JSON.net which in my case is not an option to use.

JSON.net: how to deserialize without using the default constructor?

How to deserialize class without calling a constructor?

7
  • 1
    You can't. How is the deserializer supposed to guess which constructor to use? Where would it find the parameters to pass to it? You need a default constructor for these things even if it is protected Commented Mar 21, 2016 at 12:15
  • 1
    If you need to use the XmlSerializer, then you can't... it's a limitation of this serializer Commented Mar 21, 2016 at 12:15
  • @Jcl I doubt that any serializer could work in a generic way without knowing which constructor to use. Commented Mar 21, 2016 at 12:16
  • 2
    @PanagiotisKanavos there are actually options (like FormatterServices.GetUninitializedObject), but the XmlSerializer doesn't use them :-) BinaryFormatter does use that, for example. It's definitely risky and not advisable, but it can be done Commented Mar 21, 2016 at 12:16
  • @Jcl only because it serializes entire instances, including private data that is never included in XML/Json files. Commented Mar 21, 2016 at 12:18

1 Answer 1

-2
public class Tables
{
    public Tables(object Mandate)
    {

    }

    private Tables()
    {
        // Private parameterless constructor. Leave private so that it forces everyone to use the Mandate parameter but allows serialization to work. 
    }
}
Sign up to request clarification or add additional context in comments.

7 Comments

If I'm not mistaken, the parameterless constructor for the built-in deserialization must be public.
Please explain why this answers the OP's question. Code-only answers are generally considered low quality. Given that the OP asks for a solution without a default constructor, you'd have to explain why your solution introduces a default constructor
@ShekarGurram please explain what you want in the question itself. Also post the relevant code. The deserialized would have no problem reading a nested object, so what is this object you refer to? Can you add a default constructor or not? If not, why?
@Codor No it can be private.
It results No parameterless constructor defined for type of '[object]'. exception
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.