简体   繁体   中英

C# ISerializable question

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?

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.

Example:

public Employee(SerializationInfo info, StreamingContext ctxt)
{
    //Get the values from info and assign them to the appropriate properties
    EmpId = (int)info.GetValue("EmployeeId", typeof(int));
    EmpName = (String)info.GetValue("EmployeeName", typeof(string));
}

public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
    //You can use any custom name for your name-value pair. But make sure you
    // read the values with the same name. For ex:- If you write EmpId as "EmployeeId"
    // 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.

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM