views:

247

answers:

1

So I need to serialize a generic dictionary like Dictionary<long, List<MyClass>>. I need to serialize it to store it in the ViewState of an ASP.Net application.

I found an example of using TypeConverter to convert my class to a string to be serialized, but I get an error message saying MyClass is not marked as serializable.

Here is the code for my class..

 [TypeConverter (typeof(MyClass_Converter))]
 public class MyClass
 {
     // some properties
 }

 public class MyClass_Converter : System.ComponentModel.TypeConverter
 {
     public override bool CanConvertTo(...)
     {
         // code
     }

     // CanConvertFrom, ConvertFrom, ConvertTo methods
 }

Then when I want to serialize it, I am using this code...

 LosFormatter los = new LosFormatter();
 StringWriter sw = new StringWriter();
 los.Serialize(sw, hiddenData);
 String resultSt = sw.GetStringBuilder().ToString();   
 ViewState["HiddenData"] = resultSt;  

Am I doing something wrong?

+4  A: 

Add the [Serializable] attribute to your class.
http://msdn.microsoft.com/en-us/library/system.serializableattribute.aspx

Matt Greer
So do I even need to use TypeConverter?
Eclipsed4utoo
Try it and find out, then tell us what happens.
John Saunders
I did, and I didn't see any different when using them together vs. just using Serializable.
Eclipsed4utoo