[Mono-devel-list] (De)serialization bug

Herscovici, Avi AHerscovici at Empirix.com
Tue Jul 1 09:24:32 EDT 2003


Hey all + Lluis,

  Got the latest sources and this fixed the reflection exception I got
previously.  Now to my next error :)  .. I have pasted code which seems to
have a serialization problem with types that inherit from inherited types
(children of children and so on).  This is a simplified example of our code
which throws the same exception but says Invalid Type where our code says
Unknown Type.  Hopefully a fix for this will fix our real code.  Help is
much appreciated.  (this runs fine in .net)

-Avi

// --------------------------- serial-test.cs --------------------

using System;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
using Test;


namespace Serial.test {

public class sertest {

		public static void Save(System.IO.Stream stream, object x)
		{
			XmlSerializer ser = new
XmlSerializer(typeof(Script));
			ser.Serialize(stream, x);
		}

		public static void Save(string filename, object x)
		{
			FileStream str = new FileStream(filename,
FileMode.Create, FileAccess.Write);
			Save(str, x);
			str.Close();
		}

		public static object Load(System.IO.Stream stream)
		{
			object s;
			Console.WriteLine("Debug line 1a");
			XmlSerializer ser = new
XmlSerializer(typeof(Script));
			Console.WriteLine("Debug line 2a");
		//	try {
			s = (object)ser.Deserialize(stream);
		//	}
		//	catch (Exception e) {Console.WriteLine("Exception: "
+ e);}
		//	s = null;
			return s;
		}


		public static object Load(string filename)
		{
				object s;
				Console.WriteLine("Debug line 2");
				FileStream str = new FileStream(filename,
FileMode.Open, FileAccess.Read);
				Console.WriteLine("Debug line 3");
				s = Load(str);
				Console.WriteLine("Debug line 4");
				str.Close();
				return s;
		}

	static void Main() {

	string filename = "test.ser";

	Script x =  new Script();

	x.X=0;
	x.Y=1;
	x.Z=2;
	x.String="TEST";
	Yoo tempy = new Yoo();
	tempy.A=10;
	x.Foo= tempy;
	x.Foo.I=16;

	Save(filename, x);

	Script temp = (Script)Load(filename);
	Console.WriteLine("The values are: ");
	Console.WriteLine(temp.X);
	Console.WriteLine(temp.Y);
	Console.WriteLine(temp.Z);
	Console.WriteLine(temp.String);
	Console.WriteLine(temp.Foo.I);
	}
}
}

// --------------------------- test.cs
-------------------------------------------------

using System.Xml;
using System.Xml.Serialization;

namespace Test
{
	[XmlRoot(ElementName="Script", Namespace="http://www.foo.com")]
	public class Script
	{
		// Vars
		private int m_x;
		private int m_y;
		private int m_z;
		private string m_str;
		private Foo m_foo;

		// Constructor
		public Script()
		{
		m_x = -3;
		m_y = -2;
		m_z = -1;
		m_str = null;
		m_foo = null;
		}

		// Properties
		[XmlIgnore()]
		public int X
		{
			get { return m_x; }
			set { m_x = value; }
		}

		[XmlAttribute("y")]
		public int Y
		{
			get { return m_y; }
			set { m_y = value; }
		}

		[XmlAttribute("z")]
		public int Z
		{
			get { return m_z; }
			set { m_z = value; }
		}

		[XmlAttribute("str")]
		public string String
		{
			get { return m_str; }
			set { m_str = value; }
		}
	
	
	

		public Foo Foo
		{
			get { return m_foo; }
			set { m_foo = value; }
		}
	}
	

	[XmlInclude(typeof(Bar))]
	public class Foo
	{
		private int m_i;
		public int I
		{
			get { return m_i; }
			set { m_i = value; }
		}
	}

	[XmlInclude(typeof(Yoo)),XmlInclude(typeof(Hoo))]
	public class Bar : Foo
	{
		private int m_j;
		public int J
		{
			get { return m_j; }
			set { m_j = value; }
		}
	}

	[XmlRoot("Yoo", Namespace="http://www.foo.com")]
	public class Yoo : Bar
	{
		private int m_a;
		public int A
		{
			get {return m_a;}
			set {m_a = value;}
		}
	}

	[XmlRoot("Hoo", Namespace="http://www.foo.com")]
	public class Hoo : Bar
	{
		private int m_c;
		public int C
		{
			get {return m_c;}
			set {m_c = value;}
		}
	}

	
}


// ---------- the error -------------


Unhandled Exception: System.InvalidOperationException: Invalid type:
Test.Yoo
in <0x0015b> 00
System.Xml.Serialization.XmlSerializationWriter:WriteTypedPrimitive
(string,string,object,bool)
in <0x0020a> 00
System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteObject
(System.Xml.Serialization.XmlTypeMapping,object,string,string,bool,bool,bool
)
in <0x00942> 00
System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteMemberElemen
t (System.Xml.Serialization.XmlTypeMapElementInfo,object)
in <0x0081e> 00
System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteMembers
(System.Xml.Serialization.ClassMap,object,bool)
in <0x000c8> 00
System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteObjectElemen
t (System.Xml.Serialization.XmlTypeMapping,object,string,string)
in <0x002e8> 00
System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteObject
(System.Xml.Serialization.XmlTypeMapping,object,string,string,bool,bool,bool
)
in <0x00145> 00
System.Xml.Serialization.XmlSerializationWriterInterpreter:WriteObject
(object)
in <0x0000e> 00 System.Xml.Serialization.XmlSerializer:Serialize
(object,System.Xml.Serialization.XmlSerializationWriter)
in <0x00045> 00 System.Xml.Serialization.XmlSerializer:Serialize
(System.Xml.XmlWriter,object,System.Xml.Serialization.XmlSerializerNamespace
s)
in <0x00070> 00 System.Xml.Serialization.XmlSerializer:Serialize
(System.IO.Stream,object)
in <0x00048> 00 Serial.test.sertest:Save (System.IO.Stream,object)
in <0x00049> 00 Serial.test.sertest:Save (string,object)
in <0x000fb> 00 Serial.test.sertest:Main ()



More information about the Mono-devel-list mailing list