Need Help Understanding [Serializable]

  • Thread starter Thread starter Jonathan Wood
  • Start date Start date
J

Jonathan Wood

I want to display another page in response to a postback, and I want to pass
an object to that page.

Although I could display the page with Response.Redirect() and pass the
object using Session[], I decided instead to do something like the
following. Note that pad is an instance of my custom object, PadData.

Context.Items["PadData"] = pad;
Server.Transfer("Submit_Category.aspx");

This produced the error "Error executing child request for
Submit_Category.aspx". That wasn't helpful, but if I loaded
Submit_Category.aspx directly, there was an error that PadData is not marked
as serializable.

Okay, so I added the [Serializable] tag to PadData, derived the class from
ISerializable, implemented the GetObjectData() method, and now it works!

However, there are several things about this that I don't understand.

1. Why is Serializable required here?
2. If I don't put any code in the GetObjectData method, it still works! All
pad data arrives safely at the target page. So what's the point? And is it
okay to leave this method empty?
3. Finally, why isn't SetObjectData required as well?

Thanks for any tips!

Jonathan
 
I seem to have found enough information to get by.

It appears that I can simply specify the [Serializable] attribute and it is
not necessary to derive from ISerializable. All object members are basic
types and, apparently, they get serialized just fine.

I really don't know what the attribute does.
 
Back
Top