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?

Community
  • 1
  • 1
Shekar Gurram
  • 99
  • 1
  • 5
  • 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 – Panagiotis Kanavos Mar 21 '16 at 12:15
  • 1
    If you need to use the `XmlSerializer`, then you can't... it's a limitation of this serializer – Jcl Mar 21 '16 at 12:15
  • @Jcl I doubt that *any* serializer could work in a generic way without knowing which constructor to use. – Panagiotis Kanavos Mar 21 '16 at 12:16
  • 2
    @PanagiotisKanavos there are actually options (like [FormatterServices.GetUninitializedObject](https://msdn.microsoft.com/en-us/library/system.runtime.serialization.formatterservices.getuninitializedobject.aspx)), 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 – Jcl Mar 21 '16 at 12:16
  • @Jcl only because it serializes entire instances, including private data that is never included in XML/Json files. – Panagiotis Kanavos Mar 21 '16 at 12:18
  • Yeah, I agree, I'm not saying it should be done, I'm saying it could be done (under very specific circumstances, yes) – Jcl Mar 21 '16 at 12:21
  • Cant you just add a private parameterless constructor? – CathalMF Mar 21 '16 at 12:21

1 Answers1

-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. 
    }
}
CathalMF
  • 9,705
  • 6
  • 70
  • 106
  • If I'm not mistaken, the parameterless constructor for the built-in deserialization _must_ be `public`. – Codor Mar 21 '16 at 12:26
  • 1
    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 – Panagiotis Kanavos Mar 21 '16 at 12:27
  • I need to refer to an object inside `Tables` class which will be passed in its Constructor. With param-less constructor I'll be missing it. – Shekar Gurram Mar 21 '16 at 12:27
  • 1
    @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? – Panagiotis Kanavos Mar 21 '16 at 12:30
  • @ShekarGurram In that case you cannot use XmlSerializer. It doesnt support passing arguments to constructors. You need to pick another serialization/deserialization method. – CathalMF Mar 21 '16 at 12:30
  • 1
    @Codor No it can be private. – CathalMF Mar 21 '16 at 12:31
  • 1
    It results `No parameterless constructor defined for type of '[object]'.` exception – Yusril Maulidan Raji Jan 25 '18 at 07:48