Skip to content

Commit 0c8b919

Browse files
authored
binary serialization updates (dotnet#2847)
* binary serialization updates * removed links to deleted articles * Update version-tolerant-serialization.md
1 parent d7b5b60 commit 0c8b919

14 files changed

+152
-250
lines changed

.openpublishing.redirection.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -823,6 +823,14 @@
823823
"redirect_url": "/dotnet/standard/net-standard",
824824
"redirect_document_id": true
825825
},
826+
{
827+
"source_path": "docs/standard/serialization/marshal-by-value.md",
828+
"redirect_url": "/dotnet/standard/serialization-concepts"
829+
},
830+
{
831+
"source_path": "docs/standard/serialization/persistent-storage.md",
832+
"redirect_url": "/dotnet/standard/serialization-concepts"
833+
},
826834
{
827835
"source_path": "docs/tutorials/getting-started-with-csharp/microservices.md",
828836
"redirect_url": "/dotnet/csharp/tutorials/microservices"

docs/standard/serialization/basic-serialization.md

Lines changed: 17 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,23 @@
11
---
2-
title: "Basic Serialization"
3-
ms.custom: ""
2+
title: "Basic serialization"
43
ms.date: "03/30/2017"
54
ms.prod: ".net"
6-
ms.reviewer: ""
7-
ms.suite: ""
8-
ms.tgt_pltfrm: ""
95
ms.topic: "article"
10-
dev_langs:
11-
- "VB"
12-
- "CSharp"
13-
- "C++"
14-
- "jsharp"
156
helpviewer_keywords:
167
- "binary serialization, basic serialization"
178
- "serialization, basic serialization"
189
ms.assetid: d899d43c-335a-433e-a589-cd187192984f
10+
dev_langs:
11+
- "CSharp"
1912
caps.latest.revision: 7
2013
author: "Erikre"
2114
ms.author: "erikre"
2215
manager: "erikre"
2316
---
24-
# Basic Serialization
17+
# Basic serialization
18+
19+
[!INCLUDE [binary-serialization-warning](../../../includes/binary-serialization-warning.md)]
20+
2521
The easiest way to make a class serializable is to mark it with the <xref:System.SerializableAttribute> as follows.
2622

2723
```csharp
@@ -33,7 +29,7 @@ public class MyObject {
3329
}
3430
```
3531

36-
The code example below shows how an instance of this class can be serialized to a file.
32+
The following code example shows how an instance of this class can be serialized to a file.
3733

3834
```csharp
3935
MyObject obj = new MyObject();
@@ -46,9 +42,9 @@ formatter.Serialize(stream, obj);
4642
stream.Close();
4743
```
4844

49-
This example uses a binary formatter to do the serialization. All you need to do is create an instance of the stream and the formatter you intend to use, and then call the **Serialize** method on the formatter. The stream and the object to serialize are provided as parameters to this call. Although it is not explicitly demonstrated in this example, all member variables of a class will be serialized—even variables marked as private. In this aspect, binary serialization differs from the <xref:System.Xml.Serialization.XmlSerializer> class, which only serializes public fields. For information on excluding member variables from binary serialization, see [Selective Serialization](../../../docs/standard/serialization/selective-serialization.md).
45+
This example uses a binary formatter to do the serialization. All you need to do is create an instance of the stream and the formatter you intend to use, and then call the **Serialize** method on the formatter. The stream and the object to serialize are provided as parameters to this call. Although it is not explicitly demonstrated in this example, all member variables of a class will be serialized—even variables marked as private. In this aspect, binary serialization differs from the <xref:System.Xml.Serialization.XmlSerializer> class, which only serializes public fields. For information on excluding member variables from binary serialization, see [Selective Serialization](selective-serialization.md).
5046

51-
Restoring the object back to its former state is just as easy. First, create a stream for reading and a <xref:System.Runtime.Serialization.Formatter>, and then instruct the formatter to deserialize the object. The code example below shows how this is done.
47+
Restoring the object back to its former state is just as easy. First, create a stream for reading and a <xref:System.Runtime.Serialization.Formatter>, and then instruct the formatter to deserialize the object. The code example below shows how this is done.
5248

5349
```csharp
5450
IFormatter formatter = new BinaryFormatter();
@@ -62,9 +58,9 @@ Console.WriteLine("n2: {0}", obj.n2);
6258
Console.WriteLine("str: {0}", obj.str);
6359
```
6460

65-
The <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter> used above is very efficient and produces a compact byte stream. All objects serialized with this formatter can also be deserialized with it, which makes it an ideal tool for serializing objects that will be deserialized on the .NET Framework. It is important to note that constructors are not called when an object is deserialized. This constraint is placed on deserialization for performance reasons. However, this violates some of the usual contracts the runtime makes with the object writer, and developers should ensure that they understand the ramifications when marking an object as serializable.
61+
The <xref:System.Runtime.Serialization.Formatters.Binary.BinaryFormatter> used above is very efficient and produces a compact byte stream. All objects serialized with this formatter can also be deserialized with it, which makes it an ideal tool for serializing objects that will be deserialized on the .NET Framework. It is important to note that constructors are not called when an object is deserialized. This constraint is placed on deserialization for performance reasons. However, this violates some of the usual contracts the runtime makes with the object writer, and developers should ensure that they understand the ramifications when marking an object as serializable.
6662

67-
If portability is a requirement, use the <xref:System.Runtime.Serialization.Formatters.Soap.SoapFormatter> instead. Simply replace the **BinaryFormatter** in the code above with **SoapFormatter,** and call **Serialize** and **Deserialize** as before. This formatter produces the following output for the example used above.
63+
If portability is a requirement, use the <xref:System.Runtime.Serialization.Formatters.Soap.SoapFormatter> instead. Simply replace the **BinaryFormatter** in the code above with **SoapFormatter,** and call **Serialize** and **Deserialize** as before. This formatter produces the following output for the example used above.
6864

6965
```xml
7066
<SOAP-ENV:Envelope
@@ -87,7 +83,7 @@ Console.WriteLine("str: {0}", obj.str);
8783
</SOAP-ENV:Envelope>
8884
```
8985

90-
It is important to note that the **Serializable** attribute cannot be inherited. If you derive a new class from `MyObject`, the new class must be marked with the attribute as well, or it cannot be serialized. For example, when you attempt to serialize an instance of the class below, you will get a <xref:System.Runtime.Serialization.SerializationException> informing you that the `MyStuff` type is not marked as serializable.
86+
It's important to note that the [Serializable](xref:System.SerializableAttribute) attribute cannot be inherited. If you derive a new class from `MyObject`, the new class must be marked with the attribute as well, or it cannot be serialized. For example, when you attempt to serialize an instance of the class below, you'll get a <xref:System.Runtime.Serialization.SerializationException> informing you that the `MyStuff` type is not marked as serializable.
9187

9288
```csharp
9389
public class MyStuff : MyObject
@@ -96,8 +92,8 @@ public class MyStuff : MyObject
9692
}
9793
```
9894

99-
Using the **Serializable** attribute is convenient, but it has limitations as demonstrated above. Refer to the [Serialization Guidelines](../../../docs/standard/serialization/serialization-guidelines.md) for information about when you should mark a class for serialization; serialization cannot be added to a class after it has been compiled.
95+
Using the [Serializable](xref:System.SerializableAttribute) attribute is convenient, but it has limitations as previously demonstrated. Refer to the [Serialization Guidelines](serialization-guidelines.md) for information about when you should mark a class for serialization. Serialization cannot be added to a class after it has been compiled.
10096

101-
## See Also
102-
[Binary Serialization](../../../docs/standard/serialization/binary-serialization.md)
103-
[XML and SOAP Serialization](../../../docs/standard/serialization/xml-and-soap-serialization.md)
97+
## See also
98+
[Binary Serialization](binary-serialization.md)
99+
[XML and SOAP Serialization](xml-and-soap-serialization.md)

docs/standard/serialization/binary-serialization.md

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,8 @@
11
---
2-
title: "Binary Serialization"
3-
ms.custom: ""
4-
ms.date: "03/30/2017"
2+
title: "Binary serialization"
3+
ms.date: "08/07/2017"
54
ms.prod: ".net"
6-
ms.reviewer: ""
7-
ms.suite: ""
8-
ms.tgt_pltfrm: ""
95
ms.topic: "article"
10-
dev_langs:
11-
- "VB"
12-
- "CSharp"
13-
- "C++"
14-
- "jsharp"
156
helpviewer_keywords:
167
- "binary serialization"
178
- "serialization, about serialization"
@@ -23,15 +14,17 @@ author: "Erikre"
2314
ms.author: "erikre"
2415
manager: "erikre"
2516
---
26-
# Binary Serialization
27-
Serialization can be defined as the process of storing the state of an object to a storage medium. During this process, the public and private fields of the object and the name of the class, including the assembly containing the class, are converted to a stream of bytes, which is then written to a data stream. When the object is subsequently deserialized, an exact clone of the original object is created.
28-
29-
When implementing a serialization mechanism in an object-oriented environment, you have to make a number of tradeoffs between ease of use and flexibility. The process can be automated to a large extent, provided you are given sufficient control over the process. For example, situations may arise where simple binary serialization is not sufficient, or there might be a specific reason to decide which fields in a class need to be serialized. The following sections examine the robust serialization mechanism provided with the .NET Framework and highlight a number of important features that allow you to customize the process to meet your needs.
17+
# Binary serialization
18+
Serialization can be defined as the process of storing the state of an object to a storage medium. During this process, the public and private fields of the object and the name of the class, including the assembly containing the class, are converted to a stream of bytes, which is then written to a data stream. When the object is subsequently deserialized, an exact clone of the original object is created.
19+
20+
When implementing a serialization mechanism in an object-oriented environment, you have to make a number of tradeoffs between ease of use and flexibility. The process can be automated to a large extent, provided you are given sufficient control over the process. For example, situations may arise where simple binary serialization is not sufficient, or there might be a specific reason to decide which fields in a class need to be serialized. The following sections examine the robust serialization mechanism provided with .NET and highlight a number of important features that allow you to customize the process to meet your needs.
3021

3122
> [!NOTE]
32-
> The state of a UTF-8 or UTF-7 encoded object is not preserved if the object is serialized and deserialized using different .NET Framework versions.
23+
> The state of a UTF-8 or UTF-7 encoded object is not preserved if the object is serialized and deserialized using different .NET Framework versions.
24+
25+
[!INCLUDE [binary-serialization-warning](../../../includes/binary-serialization-warning.md)]
3326

34-
## In This Section
27+
## In this section
3528
[Serialization Concepts](../../../docs/standard/serialization/serialization-concepts.md)
3629
Discusses two scenarios where serialization is useful: when persisting data to storage and when passing objects across application domains.
3730

@@ -57,7 +50,7 @@ Serialization can be defined as the process of storing the state of an object to
5750
<xref:System.Runtime.Serialization>
5851
Contains classes that can be used for serializing and deserializing objects.
5952

60-
## Related Sections
53+
## Related sections
6154
[XML and SOAP Serialization](../../../docs/standard/serialization/xml-and-soap-serialization.md)
6255
Describes the XML serialization mechanism that is included with the common language runtime.
6356

0 commit comments

Comments
 (0)