Ryan Alford
Ryan Alford

Reputation: 7594

C# Custom Serialization - Using TypeConverter

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?

Upvotes: 2

Views: 1746

Answers (1)

Matt Greer
Matt Greer

Reputation: 62057

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

Upvotes: 5

Related Questions