[Mono-list] XML Serialization - difference between WinCLR and Mono runtime

MarLOne InfoSeeker002 at gmail.com
Wed May 1 04:08:42 UTC 2013


Hi,

Not sure if this has been reported - there is difference in runtime
treatment of XML Serialization between CLR and Mono runtime. 

I have a class like this:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.Serialization;

namespace SerializationDemos
{
    [ Serializable ]
    public class WithAutoProperty
    {
        // Read only properties
        public String Name { get; private set; } // private setter
        public Int32 Age { get; private set; }     // private setter

        public WithAutoProperty() { }

        public WithAutoProperty( string name, Int32 age )
        {
            this.Name = name;
            this.Age = age;
        }
    }
}

and I have a nunit test code like this:
        [Test]
        public void WithUsingAutoProperty()
        {
            String expectedName = Guid.NewGuid().ToString();
            Int32 expectedAge = 28;
            WithAutoProperty p = new WithAutoProperty( expectedName,
expectedAge );
            UsingXmlSerialization( p, ( a, b ) =>
                    {
                        Assert.IsNotNull( a );
                        Assert.IsTrue( a is WithAutoProperty );
                        Assert.IsNotNull( b );
                        WithAutoProperty g = b as WithAutoProperty;
                        Assert.AreEqual( expectedName, g.Name );
                        Assert.AreEqual( expectedAge, g.Age );
                    } );
        }

        public void UsingXmlSerialization(Object expected, Action<object,
object> testNow)
        {
            StringBuilder buffer = new StringBuilder();
            Type t = expected.GetType();
            using ( XmlWriter writer = XmlWriter.Create( buffer ) )
            {
                XmlSerializer ser = new XmlSerializer( t );
                ser.Serialize( writer, expected );
            }

            String xmlDoc = buffer.ToString();
            Console.WriteLine( "Xml: {0}", xmlDoc );
            buffer = null;

            Object regen = null;
            using ( XmlReader reader = XmlReader.Create( new StringReader(
xmlDoc ) ) )
            {
                XmlSerializer ser = new XmlSerializer( t );
                regen = ser.Deserialize( reader );
                Assert.IsNotNull( regen );
            }
            testNow( expected, regen );
        }

I built an assembly in VS2010 using .Net4. 

Naturally according to the .Net specification, the test method
WithUsingAutoProperty() fails with a message like this:
System.InvalidOperationException : Unable to generate a temporary class
(result=1).
error CS0200: Property or indexer 'SerializationDemos.WithAutoProperty.Name'
cannot be assigned to -- it is read only
error CS0200: Property or indexer 'SerializationDemos.WithAutoProperty.Age'
cannot be assigned to -- it is read only

Then I took the assembly over to my Ubuntu 12.04 with Mono running and NUnit
ran through that method with a green bar.

This difference in behaviour is a source of concern.

Has anyone discovered this? I am using Mono-runtime version
2.10.8.1-5~dhx1~precise1.

It appears the Mono runtime simply ignoring the private accessor of the
getters and setting the value anyway in contravention to the specification.

I have the same result if I rebuild this project with MonoDevelop 3.0.3.2 in
Ubuntu 12.04. Hence it does not indicate the generation of IL code at fault
but it is in the runtime.

MarL



--
View this message in context: http://mono.1490590.n4.nabble.com/XML-Serialization-difference-between-WinCLR-and-Mono-runtime-tp4659562.html
Sent from the Mono - General mailing list archive at Nabble.com.


More information about the Mono-list mailing list