Bewerken

Delen via


BinaryFormatter.Serialize Method

Definition

Serializes an object, or graph of connected objects, to the given stream.

Overloads

Serialize(Stream, Object)
Obsolete.

Serializes the object, or graph of objects with the specified top (root), to the given stream.

Serialize(Stream, Object, Header[])

Serializes the object, or graph of objects with the specified top (root), to the given stream attaching the provided headers.

Serialize(Stream, Object)

Source:
BinaryFormatter.Core.cs
Source:
BinaryFormatter.Core.cs
Source:
BinaryFormatter.Core.cs

Caution

BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.

Serializes the object, or graph of objects with the specified top (root), to the given stream.

public:
 virtual void Serialize(System::IO::Stream ^ serializationStream, System::Object ^ graph);
[System.Obsolete("BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.", DiagnosticId="SYSLIB0011", UrlFormat="https://aka.ms/dotnet-warnings/{0}")]
public void Serialize (System.IO.Stream serializationStream, object graph);
public void Serialize (System.IO.Stream serializationStream, object graph);
[<System.Obsolete("BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information.", DiagnosticId="SYSLIB0011", UrlFormat="https://aka.ms/dotnet-warnings/{0}")>]
abstract member Serialize : System.IO.Stream * obj -> unit
override this.Serialize : System.IO.Stream * obj -> unit
abstract member Serialize : System.IO.Stream * obj -> unit
override this.Serialize : System.IO.Stream * obj -> unit
Public Sub Serialize (serializationStream As Stream, graph As Object)

Parameters

serializationStream
Stream

The stream to which the graph is to be serialized.

graph
Object

The object at the root of the graph to serialize.

Implements

Attributes

Exceptions

The serializationStream is null.

-or-

The graph is null.

An error has occurred during serialization, such as if an object in the graph parameter is not marked as serializable.

The caller does not have the required permission.

ASP.NET Core 5-8: Always thrown unless BinaryFormatter functionality is re-enabled in the project file. For more information, see Resolving BinaryFormatter obsoletion and disablement errors.

.NET 9 and later versions: In all cases. For more information, see BinaryFormatter migration guide.

Remarks

Warning

BinaryFormatter is insecure and can't be made secure. For more information, see the BinaryFormatter security guide.

Applies to

Serialize(Stream, Object, Header[])

Serializes the object, or graph of objects with the specified top (root), to the given stream attaching the provided headers.

public:
 virtual void Serialize(System::IO::Stream ^ serializationStream, System::Object ^ graph, cli::array <System::Runtime::Remoting::Messaging::Header ^> ^ headers);
public void Serialize (System.IO.Stream serializationStream, object graph, System.Runtime.Remoting.Messaging.Header[] headers);
abstract member Serialize : System.IO.Stream * obj * System.Runtime.Remoting.Messaging.Header[] -> unit
override this.Serialize : System.IO.Stream * obj * System.Runtime.Remoting.Messaging.Header[] -> unit
Public Sub Serialize (serializationStream As Stream, graph As Object, headers As Header())

Parameters

serializationStream
Stream

The stream to which the object is to be serialized.

graph
Object

The object at the root of the graph to serialize.

headers
Header[]

Remoting headers to include in the serialization. Can be null.

Implements

Exceptions

The serializationStream is null.

An error has occurred during serialization, such as if an object in the graph parameter is not marked as serializable.

The caller does not have the required permission.

Remarks

Warning

BinaryFormatter is insecure and can't be made secure. For more information, see the BinaryFormatter security guide.

The serialization process includes the information supplied in the headers parameter at the front of the serialized stream.

Headers are used only for specific remoting applications.

Applies to