none
XmlSerializer does NOT need a public default constructor anymore? RRS feed

  • Question

  • Doing a demo (which i've done many times) about the XmlSerializer, something hit me yesterday (1st March 2010).

    The XmlSerializer does NOT require a public default constructor. This is a change from all my previous experiences. It always failed when no pub-def ctor was available. Now it works fine (.Net 3.5 SP1).

    I've been trying to find the documentation about this change (and when it changed) but am unable to do so.

    Could someone tell me when (which release) this requirement of the XmlSerializer changed?
    Is this a side-effect of another change or the new intended functionality?

    Regards,

    Marvin Smit
    Another bitcode bytes the dust
    Tuesday, March 2, 2010 9:41 AM

Answers

  • Hello Marvin,

    I searched on the web and found the following KB article which describes this issue. So I copied the codes from there,
    http://support.microsoft.com/kb/330592

    That is why I use [Serializable] attribute on the top of my SerializerTest class.

    Currently, based on my test, even for .NET 2.0 project, if we have a private parameterless constructor, we can serialize and deserialize this kind of object to and from XML file correctly. So we do not need it to be public. As my understanding, even if a method is private, it can be called by Refelection. That would be enough for .NET to initilize an instance and set properties retrieving from XML file.
    http://stackoverflow.com/questions/135443/how-do-i-use-reflection-to-invoke-a-private-method-in-c

    Which version of .NET you find the constructor must be public?


    Best regards,
    Ji Zhou - MSFT
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, March 9, 2010 6:09 AM
    Moderator

All replies

  • Hello Marvin,

    It does not change. We still need a default parameterless constructor for correctly serializing an object.

    We just do not encounter the error in the compile time, but if we run the codes, we will receive an runtime error,

    InvalidOperationException was unhandled,
    SerializerTest cannot be serialized because it does not have a parameterless constructor.

    ---------------------------------------
        [Serializable]
        public class SerializerTest
        {
            public SerializerTest(string data)
            {
                this.data = data;
            }

            public string data;

             }
       
        class Program
        {
            static void Main(string[] args)
            {
                SerializerTest testInstance = new SerializerTest("Hello World!");

                XmlSerializer ser = new XmlSerializer(typeof(SerializerTest));

                TextWriter writer = new StreamWriter("C:\\test.xml");
                ser.Serialize(writer, testInstance);
                writer.Close();
            }
        }
    ---------------------------------------

    Best regards,
    Ji Zhou
    MSDN Subscriber Support in Forum


    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Thursday, March 4, 2010 5:00 AM
    Moderator
  • Hi Ji,

    thank you for your response. Maybe i should eleborate.

    I'm aware that we need a parameterless contrstructor for the XmlSerializer, i was just under the impression is had to be 'Public', which is no longer the case. Private parameterless constructors work fine, and thats my real question. Did the XmlSerializer 'used to' require a 'public' ctor, while in the latest version (using 3.5SP1) you can have the ctor being private. (Thereby also being able to serialize a Singleton for instance).

    ps; Why do you use the [Serializable] attribute on top of your SerializerTest class? Isn't that for the Runtime serializers?


    Another bitcode bytes the dust
    • Edited by Marvin Smit Thursday, March 4, 2010 8:58 AM Added question about attribute
    Thursday, March 4, 2010 8:41 AM
  • Hello Marvin,

    I searched on the web and found the following KB article which describes this issue. So I copied the codes from there,
    http://support.microsoft.com/kb/330592

    That is why I use [Serializable] attribute on the top of my SerializerTest class.

    Currently, based on my test, even for .NET 2.0 project, if we have a private parameterless constructor, we can serialize and deserialize this kind of object to and from XML file correctly. So we do not need it to be public. As my understanding, even if a method is private, it can be called by Refelection. That would be enough for .NET to initilize an instance and set properties retrieving from XML file.
    http://stackoverflow.com/questions/135443/how-do-i-use-reflection-to-invoke-a-private-method-in-c

    Which version of .NET you find the constructor must be public?


    Best regards,
    Ji Zhou - MSFT
    MSDN Subscriber Support in Forum
    If you have any feedback on our support, please contact msdnmg@microsoft.com
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.
    Tuesday, March 9, 2010 6:09 AM
    Moderator
  • Hi Ji,

    thank you for your response. Although there is an example code behind your link of the XmlSerializer, it does not say anything about the [Serializable] attribute. According to me, this is still not needed for the XmlSerializer, only the runtime formatters. (Ps; this article also stated "Public default constructor", not just 'any' default constructor.)

    I've tried to find a vanilla .Net 2.0 environment to do some more testing code of my own before coming back to this post. I've not been able to do so. As soon as I have the availability of that, i will retest and post it here. 

    Another bitcode bytes the dust
    Thursday, March 11, 2010 9:25 AM