I found out that some classes use the [Serializable]
attribute.
我发现有些类使用[Serializable]属性。
- What is it?
- 它是什么?
- When should I use it?
- 我应该什么时候使用它?
- What kinds of benefits will I get?
- 我能得到什么好处?
6 个解决方案
#1
296
What is it?
When you create an object in a .Net framework application, you don't need to think about how the data is stored in memory. Because the .Net Framework takes care of that for you. However, if you want to store the contents of an object to a file, send an object to another process or transmit it across the network, you do have to think about how the object is represented because you will need to convert to a different format. This conversion is called SERIALIZATION.
在. net framework应用程序中创建对象时,不需要考虑数据是如何存储在内存中的。因为。net框架会帮你解决这个问题。但是,如果您想将对象的内容存储到文件中、将对象发送到另一个进程或通过网络传输,您必须考虑如何表示对象,因为您将需要转换到另一种格式。这种转换称为序列化。
Uses for Serialization
Serialization allows the developer to save the state of an object and recreate it as needed, providing storage of objects as well as data exchange. Through serialization, a developer can perform actions like sending the object to a remote application by means of a Web Service, passing an object from one domain to another, passing an object through a firewall as an XML string, or maintaining security or user-specific information across applications.
序列化允许开发人员保存对象的状态并根据需要重新创建它,提供对象的存储和数据交换。通过序列化,开发人员可以执行一些操作,比如通过Web服务将对象发送到远程应用程序,将对象从一个域传递到另一个域,将对象作为XML字符串传递到防火墙,或者跨应用程序维护安全性或特定于用户的信息。
Apply SerializableAttribute
to a type to indicate that instances of this type can be serialized. Apply the SerializableAttribute
even if the class also implements the ISerializable
interface to control the serialization process.
将SerializableAttribute应用于一个类型,以表明可以序列化该类型的实例。即使类也实现了ISerializable接口来控制序列化过程,也要应用SerializableAttribute。
All the public and private fields in a type that are marked by the SerializableAttribute
are serialized by default, unless the type implements the ISerializable
interface to override the serialization process. The default serialization process excludes fields that are marked with NonSerializedAttribute
. If a field of a serializable type contains a pointer, a handle, or some other data structure that is specific to a particular environment, and cannot be meaningfully reconstituted in a different environment, then you might want to apply NonSerializedAttribute
to that field.
由SerializableAttribute标记的类型中的所有公共和私有字段默认被序列化,除非该类型实现ISerializable接口来覆盖序列化过程。默认的序列化过程排除了标记为非serializedattribute的字段。如果serializable类型的字段包含指针、句柄或特定于特定环境的其他数据结构,并且不能在不同的环境中进行有意义的重构,那么您可能希望对该字段应用非serializedattribute。
See MSDN for more details.
详情请参阅MSDN。
Edit 1
编辑1
Any reason to not mark something as serializable
没有任何理由不标记一些可序列化的东西。
When transferring or saving data, you need to send or save only the required data. So there will be less transfer delays and storage issues. So you can opt out unnecessary chunk of data when serializing.
在传输或保存数据时,只需要发送或保存所需的数据。因此,传输延迟和存储问题将会减少。因此,在序列化时可以选择不需要的数据块。
#2
32
Some practical uses for the [Serializable]
attribute:
[Serializable]属性的一些实际用途:
- Saving object state using binary serialisation; you can very easily 'save' entire object instances in your application to a file or network stream and then recreate them by deserialising - check out the
BinaryFormatter
class in System.Runtime.Serialization.Formatters.Binary - 使用二进制序列化保存对象状态;您可以很容易地将应用程序中的整个对象实例“保存”到文件或网络流中,然后通过反序列化重新创建它们——请查看system . runtime. serializ.formatters.binary中的BinaryFormatter类
- Writing classes whose object instances can be stored on the clipboard using
Clipboard.SetData()
- nonserialisable classes cannot be placed on the clipboard. - 使用clipboard. setdata()编写对象实例可以存储在剪贴板上的类——不可序列化的类不能放在剪贴板上。
- Writing classes which are compatible with .NET Remoting; generally, any class instance you pass between application domains (except those which extend from
MarshalByRefObject
) must be serialisable. - 编写与.NET Remoting兼容的类;通常,您在应用程序域之间传递的任何类实例(除了从MarshalByRefObject扩展的类实例)都必须是可序列化的。
These are the most common usage cases that I have come across.
这些是我遇到的最常见的用法。
#3
17
Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file.
串行化是将对象转换成字节流的过程,以便存储对象或将其传输到内存、数据库或文件。
How Serialization Works
序列化是如何工作的
This illustration shows the overall process of serialization.
这个例子展示了序列化的整个过程。
The object is serialized to a stream, which carries not just the data, but information about the object's type, such as its version, culture, and assembly name. From that stream, it can be stored in a database, a file, or memory.
对象被序列化为流,流不仅包含数据,还包含关于对象类型的信息,如其版本、区域性和程序集名称。从该流中,它可以存储在数据库、文件或内存中。
在msdn细节。
#4
16
Here is short example of how serialization works. I was also learning about the same and I found two links useful. What Serialization is and how it can be done in .NET.
下面是序列化如何工作的简短示例。我也学到了同样的东西,我发现两个链接很有用。序列化是什么,如何在。net中执行。
A sample program explaining serialization
解释序列化的示例程序
If you don't understand the above program a much simple program with explanation is given here.
如果您不理解上面的程序,这里给出了一个非常简单的程序和说明。
#5
5
Serialization
序列化
Serialization is the process of converting an object or a set of objects graph into a stream, it is a byte array in the case of binary serialization
序列化是将一个对象或一组对象图转换成流的过程,它是二进制序列化的字节数组
Uses of Serialization
使用串行化
- To save the state of an object into a file, database etc. and use it latter.
- 将对象的状态保存到文件、数据库等中,并使用后者。
- To send an object from one process to another (App Domain) on the same machine and also send it over wire to a process running on another machine.
- 将一个对象从一个进程发送到同一台机器上的另一个进程(App域),并将其通过网络发送到另一台机器上运行的进程。
- To create a clone of the original object as a backup while working on the main object.
- 在处理主对象时创建原始对象的克隆作为备份。
- A set of objects can easily be copied to the system’s clipboard and then pasted into the same or another application
- 一组对象可以很容易地复制到系统的剪贴板,然后粘贴到相同或另一个应用程序中。
Below are some useful custom attributes that are used during serialization of an object
下面是在对象序列化过程中使用的一些有用的自定义属性
[Serializable] -> It is used when we mark an object’s serializable [NonSerialized] -> It is used when we do not want to serialize an object’s field. [OnSerializing] -> It is used when we want to perform some action while serializing an object [OnSerialized] -> It is used when we want to perform some action after serialized an object into stream.
>当我们标记一个对象的可序列化[非序列化]->时使用,当我们不想序列化一个对象的字段时使用。>当我们要在序列化一个对象[OnSerialized]时执行某个操作时使用——>当我们要在将一个对象序列化成流后执行某个操作时使用。
Below is the example of serialization
下面是序列化的示例
[Serializable]
internal class DemoForSerializable
{
internal string Fname = string.Empty;
internal string Lname = string.Empty;
internal Stream SerializeToMS(DemoForSerializable demo)
{
DemoForSerializable objSer = new DemoForSerializable();
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, objSer);
return ms;
}
[OnSerializing]
private void OnSerializing(StreamingContext context) {
Fname = "sheo";
Lname = "Dayal";
}
[OnSerialized]
private void OnSerialized(StreamingContext context)
{
// Do some work after serialized object
}
}
Here is the calling code
这是调用代码
class Program
{
string fname = string.Empty;
string Lname = string.Empty;
static void Main(string[] args)
{
DemoForSerializable demo = new DemoForSerializable();
Stream ms = demo.SerializeToMS(demo);
ms.Position = 0;
DemoForSerializable demo1 = new BinaryFormatter().Deserialize(ms) as DemoForSerializable;
Console.WriteLine(demo1.Fname);
Console.WriteLine(demo1.Lname);
Console.ReadLine();
}
}
#6
1
Since the original question was about the SerializableAttribute, it should be noted that this attribute only applies when using the BinaryFormatter or SoapFormatter.
由于最初的问题是关于SerializableAttribute的,应该注意的是,这个属性只适用于使用BinaryFormatter或SoapFormatter。
It is a bit confusing, unless you really pay attention to the details, as to when to use it and what its actual purpose is.
这有点让人困惑,除非你真正关注细节,比如何时使用它以及它的实际用途是什么。
It has NOTHING to do with XML or JSON serialization.
它与XML或JSON序列化毫无关系。
Used with the SerializableAttribute are the ISerializable Interface and SerializationInfo Class. These are also only used with the BinaryFormatter or SoapFormatter.
与SerializableAttribute一起使用的是ISerializable接口和SerializationInfo类。它们也只用于BinaryFormatter或SoapFormatter。
Unless you intend to serialize your class using Binary or Soap, do not bother marking your class as [Serializable]. XML and JSON serializers are not even aware of its existence.
除非您打算使用二进制或Soap序列化您的类,否则不要麻烦将您的类标记为[Serializable]。XML和JSON序列化器甚至不知道它的存在。
#1
296
What is it?
When you create an object in a .Net framework application, you don't need to think about how the data is stored in memory. Because the .Net Framework takes care of that for you. However, if you want to store the contents of an object to a file, send an object to another process or transmit it across the network, you do have to think about how the object is represented because you will need to convert to a different format. This conversion is called SERIALIZATION.
在. net framework应用程序中创建对象时,不需要考虑数据是如何存储在内存中的。因为。net框架会帮你解决这个问题。但是,如果您想将对象的内容存储到文件中、将对象发送到另一个进程或通过网络传输,您必须考虑如何表示对象,因为您将需要转换到另一种格式。这种转换称为序列化。
Uses for Serialization
Serialization allows the developer to save the state of an object and recreate it as needed, providing storage of objects as well as data exchange. Through serialization, a developer can perform actions like sending the object to a remote application by means of a Web Service, passing an object from one domain to another, passing an object through a firewall as an XML string, or maintaining security or user-specific information across applications.
序列化允许开发人员保存对象的状态并根据需要重新创建它,提供对象的存储和数据交换。通过序列化,开发人员可以执行一些操作,比如通过Web服务将对象发送到远程应用程序,将对象从一个域传递到另一个域,将对象作为XML字符串传递到防火墙,或者跨应用程序维护安全性或特定于用户的信息。
Apply SerializableAttribute
to a type to indicate that instances of this type can be serialized. Apply the SerializableAttribute
even if the class also implements the ISerializable
interface to control the serialization process.
将SerializableAttribute应用于一个类型,以表明可以序列化该类型的实例。即使类也实现了ISerializable接口来控制序列化过程,也要应用SerializableAttribute。
All the public and private fields in a type that are marked by the SerializableAttribute
are serialized by default, unless the type implements the ISerializable
interface to override the serialization process. The default serialization process excludes fields that are marked with NonSerializedAttribute
. If a field of a serializable type contains a pointer, a handle, or some other data structure that is specific to a particular environment, and cannot be meaningfully reconstituted in a different environment, then you might want to apply NonSerializedAttribute
to that field.
由SerializableAttribute标记的类型中的所有公共和私有字段默认被序列化,除非该类型实现ISerializable接口来覆盖序列化过程。默认的序列化过程排除了标记为非serializedattribute的字段。如果serializable类型的字段包含指针、句柄或特定于特定环境的其他数据结构,并且不能在不同的环境中进行有意义的重构,那么您可能希望对该字段应用非serializedattribute。
See MSDN for more details.
详情请参阅MSDN。
Edit 1
编辑1
Any reason to not mark something as serializable
没有任何理由不标记一些可序列化的东西。
When transferring or saving data, you need to send or save only the required data. So there will be less transfer delays and storage issues. So you can opt out unnecessary chunk of data when serializing.
在传输或保存数据时,只需要发送或保存所需的数据。因此,传输延迟和存储问题将会减少。因此,在序列化时可以选择不需要的数据块。
#2
32
Some practical uses for the [Serializable]
attribute:
[Serializable]属性的一些实际用途:
- Saving object state using binary serialisation; you can very easily 'save' entire object instances in your application to a file or network stream and then recreate them by deserialising - check out the
BinaryFormatter
class in System.Runtime.Serialization.Formatters.Binary - 使用二进制序列化保存对象状态;您可以很容易地将应用程序中的整个对象实例“保存”到文件或网络流中,然后通过反序列化重新创建它们——请查看system . runtime. serializ.formatters.binary中的BinaryFormatter类
- Writing classes whose object instances can be stored on the clipboard using
Clipboard.SetData()
- nonserialisable classes cannot be placed on the clipboard. - 使用clipboard. setdata()编写对象实例可以存储在剪贴板上的类——不可序列化的类不能放在剪贴板上。
- Writing classes which are compatible with .NET Remoting; generally, any class instance you pass between application domains (except those which extend from
MarshalByRefObject
) must be serialisable. - 编写与.NET Remoting兼容的类;通常,您在应用程序域之间传递的任何类实例(除了从MarshalByRefObject扩展的类实例)都必须是可序列化的。
These are the most common usage cases that I have come across.
这些是我遇到的最常见的用法。
#3
17
Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file.
串行化是将对象转换成字节流的过程,以便存储对象或将其传输到内存、数据库或文件。
How Serialization Works
序列化是如何工作的
This illustration shows the overall process of serialization.
这个例子展示了序列化的整个过程。
The object is serialized to a stream, which carries not just the data, but information about the object's type, such as its version, culture, and assembly name. From that stream, it can be stored in a database, a file, or memory.
对象被序列化为流,流不仅包含数据,还包含关于对象类型的信息,如其版本、区域性和程序集名称。从该流中,它可以存储在数据库、文件或内存中。
在msdn细节。
#4
16
Here is short example of how serialization works. I was also learning about the same and I found two links useful. What Serialization is and how it can be done in .NET.
下面是序列化如何工作的简短示例。我也学到了同样的东西,我发现两个链接很有用。序列化是什么,如何在。net中执行。
A sample program explaining serialization
解释序列化的示例程序
If you don't understand the above program a much simple program with explanation is given here.
如果您不理解上面的程序,这里给出了一个非常简单的程序和说明。
#5
5
Serialization
序列化
Serialization is the process of converting an object or a set of objects graph into a stream, it is a byte array in the case of binary serialization
序列化是将一个对象或一组对象图转换成流的过程,它是二进制序列化的字节数组
Uses of Serialization
使用串行化
- To save the state of an object into a file, database etc. and use it latter.
- 将对象的状态保存到文件、数据库等中,并使用后者。
- To send an object from one process to another (App Domain) on the same machine and also send it over wire to a process running on another machine.
- 将一个对象从一个进程发送到同一台机器上的另一个进程(App域),并将其通过网络发送到另一台机器上运行的进程。
- To create a clone of the original object as a backup while working on the main object.
- 在处理主对象时创建原始对象的克隆作为备份。
- A set of objects can easily be copied to the system’s clipboard and then pasted into the same or another application
- 一组对象可以很容易地复制到系统的剪贴板,然后粘贴到相同或另一个应用程序中。
Below are some useful custom attributes that are used during serialization of an object
下面是在对象序列化过程中使用的一些有用的自定义属性
[Serializable] -> It is used when we mark an object’s serializable [NonSerialized] -> It is used when we do not want to serialize an object’s field. [OnSerializing] -> It is used when we want to perform some action while serializing an object [OnSerialized] -> It is used when we want to perform some action after serialized an object into stream.
>当我们标记一个对象的可序列化[非序列化]->时使用,当我们不想序列化一个对象的字段时使用。>当我们要在序列化一个对象[OnSerialized]时执行某个操作时使用——>当我们要在将一个对象序列化成流后执行某个操作时使用。
Below is the example of serialization
下面是序列化的示例
[Serializable]
internal class DemoForSerializable
{
internal string Fname = string.Empty;
internal string Lname = string.Empty;
internal Stream SerializeToMS(DemoForSerializable demo)
{
DemoForSerializable objSer = new DemoForSerializable();
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, objSer);
return ms;
}
[OnSerializing]
private void OnSerializing(StreamingContext context) {
Fname = "sheo";
Lname = "Dayal";
}
[OnSerialized]
private void OnSerialized(StreamingContext context)
{
// Do some work after serialized object
}
}
Here is the calling code
这是调用代码
class Program
{
string fname = string.Empty;
string Lname = string.Empty;
static void Main(string[] args)
{
DemoForSerializable demo = new DemoForSerializable();
Stream ms = demo.SerializeToMS(demo);
ms.Position = 0;
DemoForSerializable demo1 = new BinaryFormatter().Deserialize(ms) as DemoForSerializable;
Console.WriteLine(demo1.Fname);
Console.WriteLine(demo1.Lname);
Console.ReadLine();
}
}
#6
1
Since the original question was about the SerializableAttribute, it should be noted that this attribute only applies when using the BinaryFormatter or SoapFormatter.
由于最初的问题是关于SerializableAttribute的,应该注意的是,这个属性只适用于使用BinaryFormatter或SoapFormatter。
It is a bit confusing, unless you really pay attention to the details, as to when to use it and what its actual purpose is.
这有点让人困惑,除非你真正关注细节,比如何时使用它以及它的实际用途是什么。
It has NOTHING to do with XML or JSON serialization.
它与XML或JSON序列化毫无关系。
Used with the SerializableAttribute are the ISerializable Interface and SerializationInfo Class. These are also only used with the BinaryFormatter or SoapFormatter.
与SerializableAttribute一起使用的是ISerializable接口和SerializationInfo类。它们也只用于BinaryFormatter或SoapFormatter。
Unless you intend to serialize your class using Binary or Soap, do not bother marking your class as [Serializable]. XML and JSON serializers are not even aware of its existence.
除非您打算使用二进制或Soap序列化您的类,否则不要麻烦将您的类标记为[Serializable]。XML和JSON序列化器甚至不知道它的存在。