[Mono-devel-list] XmlSerializer

Lluis Sanchez lluis at ximian.com
Mon Jul 12 04:17:40 EDT 2004


Can you file a bug report in bugzilla.ximian.com with this info?
Thanks!

On ds, 2004-07-10 at 23:45, radek polak wrote:
> Hello,
> this code works under MS.NET, but throws following exception on mono beta3:
> 
> System.InvalidOperationException: SerializationTest.MyList cannot be serialized because it does not have a default public constructor
> 
> Exception under Mono seems logical to me. I am trying to serialize MyList class, that has no public constructor. But how is it possible to work under MS.NET? The reason must be, that MyList inherits from ArrayList.
> 
> Any ideas? Thanks. Radek.
> 
> using System;
> using System.IO;
> using System.Collections;
> using System.Xml.Serialization;
> 
> namespace SerializationTest
> {
> 	[Serializable]
> 	public class MyList : ArrayList
> 	{
> 		Container container;	// in real implementation i need to know "my container"
> 
> 		// NOTE: MyList has no public constructor
> 		public MyList(Container container) : base() {
> 			this.container = container;
> 		}
> 	}
> 
> 	[Serializable]
> 	public class Container
> 	{
> 		public MyList Items;
> 
> 		public Container() {
> 			Items = new MyList(this);
> 		}
> 
> 		public void Save(Stream s) {
> 			XmlSerializer serializer = new XmlSerializer(this.GetType());
> 			serializer.Serialize(s, this);
> 		}
> 
> 		public static Container FromStream(Stream s) {
> 			XmlSerializer serializer = new XmlSerializer(typeof(Container));
> 			Container c = (Container) serializer.Deserialize(s);
> 			return c;
> 		}
> 
> 		public static void Main()
> 		{
> 			Container c = new Container();
> 			c.Items.Add(1);
> 			using(FileStream fs = new FileStream("container.xml", FileMode.Create)) {
> 				c.Save(fs);
> 			}
> 
> 			Container fromFile = Container.FromStream(File.OpenRead("container.xml"));
> 			Console.WriteLine("It works, first item is " + fromFile.Items[0]);
> 			Console.ReadLine();
> 		}
> 	}
> }
> ____________________________________________________________
> Příjemný design. Příjemnější cena. SONY ERICSSON T105 již od 577 Kč
> http://ad.seznam.cz/clickthru?spotId=75826




More information about the Mono-devel-list mailing list