简体   繁体   English

C#ISerializable问题

[英]C# ISerializable question

I am planning to use serialization to do clone. 我打算用序列化做克隆。 I have to make my class ISerializable. 我必须让我的课程ISerializable。 But how about its super classes and all referenced variable classes? 但是它的超级类和所有引用的变量类呢? do I need to make them all ISerializable? 我需要将它们全部变为ISerializable吗?

If I use ISerializable. 如果我使用ISerializable。 I have to implement the GetObjectData(), what I should put inside that method? 我必须实现GetObjectData(),我应该放在该方法中? 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. 除非使用[Serializable]属性标记类,否则此接口必须由具有序列化实例的所有类实现。 Use the ISerializable interface if you want your class to control its own serialization and de-serialization. 如果希望类控制自己的序列化和反序列化,请使用ISerializable接口。

The GetObjectData() let you control the serialization process. GetObjectData()允许您控制序列化过程。

GetDataObject, to which you pass a SerializationInfo object and a StreamingContext object. GetDataObject,您向其传递SerializationInfo对象和StreamingContext对象。 The GetDataObject method will then populate the SerializationInfo object with the data necessary for the serialization of the target object. 然后,GetDataObject方法将使用序列化目标对象所需的数据填充SerializationInfo对象。

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. 如您所见,反序列化需要GetObjectData。 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. 有两种方法可以使您的类型在.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. 最简单和最简单的方法是使用Serializable属性标记您的类型,层次结构中所有相关类型以及这些类型中所有类型的所有字段。 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. 您只需将类型声明为可序列化,CLR即可完成其余工作。

The other way is to implement ISerializable on all of your types in the hierarchy. 另一种方法是在层次结构中的所有类型上实现ISerializable 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. 实际上,您必须在ISerializable上实现方法以手动序列化值。 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 . 根据您的场景,我将使用Serializable属性路由,除非您有使用ISerializable的非常具体的原因。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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