jQuery and ASP.NET

April 29, 2009

Can my class be serialized?




A common question asked is how do you determine if a class can be serialized? My answer is - serialize the class and then determine.

A common misconception is that if the class has the [Serializable] attribute, it can be serialized. This may or may not be the case. If any one of the fields in the class cannot be serialized, the serialization of the class fails.

If you would like to programmatically determine if a class can probably be serialized, use the following code. The code determines if the class Employee can be serialized or not:

C#


        Employee emp = new Employee();


        if (emp.GetType().IsSerializable)


        {


            Response.Write(emp.GetType().ToString() + " can probably be serialized");


        }


        else


        {


            Response.Write(emp.GetType().ToString() + " cannot be serialized");


        }




VB.NET


Dim emp As New Employee()


If emp.GetType().IsSerializable Then


    Response.Write(emp.GetType().ToString() & " can probably be serialized")


Else


    Response.Write(emp.GetType().ToString() & " cannot be serialized")


End If



Bookmark this link on del.icio.us (saved by 0 users)

Did you like this post?
kick it on DotNetKicks.com
subscribe via rss subscribe via e-mail
print this post follow me on twitter
Others Also Read..

comments

0 Responses to "Can my class be serialized?"
 

Copyright 2010 All Rights Reserved DevCurry.com by Suprotim Agarwal