开发者

C# Custom Serialization - Using TypeConverter

开发者 https://www.devze.com 2022-12-15 04:04 出处:网络
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.

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?


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

0

精彩评论

暂无评论...
验证码 换一张
取 消