有标记接口,如Java的序列化或Cloneable的设计在C#中演变? [英] Has the design of marker interfaces like Java's Serializable or Cloneable evolved in C#?
问题描述
Java提供的java.io.Serializable
和 java.lang.Cloneable
在他的标准库(和特殊的支持它在周边的反序列化/序列化/克隆任务语言和JVM)。
Java provides java.io.Serializable
and java.lang.Cloneable
in his standard library (and special support for it in the language and the JVM) for tasks around deserializing/serializing/cloning.
选择了一条不同的路径来提供这种功能的C#,请问该怎么实现和代码用它与Java不同,为什么却做了这样?
Has C# chosen a different path to provide this functionality, how does the implementation and code using it differ from Java and why was it done this way?
作为一个例子,为什么C#同时使用一个属性(注释)和串行接口?
As an example, why does C# use both an attribute (annotation) and an interface for serialization?
推荐答案
.NET不使用 ISerializable的
只是一个标记接口。它不仅作为一个标记,也可以让你精确控制.NET如何通过实现序列化类 GetObjectData使用
和一个构造函数,采取适当的参数。
.NET doesn't use ISerializable
as just a marker interface. It acts not only as a marker, but also allows you to control exactly how .NET will serialize the class by implementing GetObjectData
and a constructor which takes the suitable arguments.
属性时使用的类可序列化,但你不想定义自己的序列化行为。
The attribute is used when the class can be serialized, but you don't want to define your own serialization behavior.
因此:使用 ISerializable的
当你要定义自己的序列化行为;或者当你要离开它的序列化格式使用 [Serializable接口]
属性。
So: Use ISerializable
when you want to define your own serialization behavior; or use the [Serializable]
attribute when you want to leave it up to the serialization formatter.
我称之为它的演变?我不知道。 .NET仅仅是为您提供不同程度的灵活性。
Would I call it evolution? I don't know. .NET is just offering you different degrees of flexibility.
这篇关于有标记接口,如Java的序列化或Cloneable的设计在C#中演变?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!