Nicholas Paldino [.NET/C# MVP] wrote:
The HashTable is indeed serializable
! That surprized me.
Ok, to give some context I am assuming:
==MyClass.cs:
using System;
using System.Collections;
using System.Xml;
using System.Xml.Serialization;
namespace TestHashtableSerialization
{
[XmlRoot(ElementName="MyClass")]
public class MyClass
{
[XmlElement(ElementName="Table")]
public Hashtable table = new Hashtable();
public MyClass()
{ }
}
}
==End of MyClass.cs
==Start.cs (I am skipping the usual code that comes with forms)
using System;
using System.Collections;
using System.IO;
using System.Xml;
using System.Xml.Serialization;
namespace TestHashtableSerialization
{
public class Start : System.Windows.Forms.Form
{
public Start()
{
// Required for Windows Form Designer support
InitializeComponent();
}
[STAThread]
static void Main()
{
Application.Run(new Start());
}
private void btn_Test_Click(object sender, System.EventArgs e)
{
try
{
MyClass c = new MyClass();
c.table[0]="zeroth";
c.table[1]="first";
XmlSerializer serializer = new
XmlSerializer(typeof(TestHashtableSerialization.My Class));
TextWriter writer = new StreamWriter("SerializedMyClass.xml");
serializer.Serialize(writer, c);
writer.Close();
MessageBox.Show(this, "MyClass serialized.");
}
catch(Exception e2)
{
MessageBox.Show(this,
"Exception:\n"+e2.ToString());
}
}
}
}
==End of Start.cs
Running this and clicking on the btn_Test button I get an exception:
System.InvalidOperationException: There was an error reflecting type
'TestHashtableSerialization.MyClass'.
---> System.NotSupportedException: The type
System.Collections.Hashtable is not supported because it implements
IDictionary.
So, assuming we were talking about Serialization to Xml files via
XmlSerializer...
....if, as you say, Hashtables ARE Serializable 'out-of-the-box', what
am I missing/doing wrong?
I am *Very Interested*,
thanks,
F.O.R.