Take the 2-minute tour ×

I am planning to use serialization to do clone. I have to make my class ISerializable. But how about its super classes and all referenced variable classes? do I need to make them all ISerializable?

If I use ISerializable. I have to implement the GetObjectData(), what I should put inside that method? leave it empty is ok?

share|improve this question

2 Answers 2

up vote 9 down vote accepted

Unless you mark the class with the [Serializable] attribute, this interface must be implemented by all classes with serialized instances. Use the ISerializable interface if you want your class to control its own serialization and de-serialization.

The GetObjectData() let you control the serialization process.

GetDataObject, to which you pass a SerializationInfo object and a StreamingContext object. The GetDataObject method will then populate the SerializationInfo object with the data necessary for the serialization of the target object.


public Employee(SerializationInfo info, StreamingContext ctxt)

public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
    // then you should read the same with "EmployeeId"
    info.AddValue("EmployeeId", EmpId);
    info.AddValue("EmployeeName", EmpName);

The example above show you how to deserialize and serialize. As you can see GetObjectData is required to deserialize. If you leave it empty, your object won't have the desire values.

share|improve this answer
what I try to do is use this approach to do clone: stackoverflow.com/questions/78536/cloning-objects-in-c. If I need to define all the GetObjectData method, it may be better to define my own Clone method. –  5YrsLaterDBA Mar 19 '10 at 17:14
You could simply add the [Serializable] tag above the class. This way your object will be serializable. Use the Clone() method like in the link you sent me and you should be in business :) –  Patrick Desjardins Mar 19 '10 at 17:16
Your blockquote uses "GetDataObject" whereas your code and other text has "GetObjectData". Are these two separate methods, or is this just a typo? –  Conspicuous Compiler Mar 19 '10 at 17:26
if I have an ArrayList member, will it be deep copied by default if I use that serialable approach? –  5YrsLaterDBA Mar 19 '10 at 18:43
GetObjectData is required to serialize. The constructor is required to deserialize. –  Suncat2000 Apr 18 at 16:00

There are 2 ways to make your types binary serializable in .Net.

The simplest and easiest way is to tag your type, all it's associated types in the hierarchy and all types of all fields in those types with the Serializable attribute. This seems like a lot but for small object hierarchies it cant be done relatively quickly. What's nice about this approach though is that you don't actually have to do any other work for serialization. You simply declare the types to be serializable and the CLR does the rest of the work.

The other way is to implement ISerializable on all of your types in the hierarchy. But all this does is declare your type to be serializable. You actually have to implement the methods on ISerializable to manually serialize the values. Generally speaking this approach is a lot more work and has added maintenance costs as you must constantly update it when fields are addded or removed from a type. It can be necessary though if you don't control all types in the hierarchy

Base on your scenario I would go the Serializable attribute route unless you have a very specific reason for using ISerializable.

share|improve this answer

Your Answer


