[Mono-devel-list] XmlSerializer

radek polak psonek2 at seznam.cz
Sat Jul 10 17:45:47 EDT 2004


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
-------------- next part --------------
A non-text attachment was scrubbed...
Name: SerializationTest.cs
Type: text/x-csharp
Size: 1196 bytes
Desc: not available
Url : http://lists.ximian.com/pipermail/mono-devel-list/attachments/20040710/a2cfe29e/attachment.bin 


More information about the Mono-devel-list mailing list