开发者

C# ISerializable question

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

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(), wha开发者_Python百科t 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.

0

精彩评论

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