Streaming Context structure Provides information about the source and destination of a specific serialized stream

While working with objects, you need to implement serialization in some form to save information in the object or to move the object to another process. Serialization is a functionality that is critical while building a distributed application. You can use the built-in classes or create custom classes to serialize data in the distributed application.

While implementing custom serialization and deserialization functionalities in a class, you need to take care of two things. During the serialization of an object, you need to specify the values of the object to be serialized, and during deserialization, you need to collect values and other information about the object present in the serialized stream. The .NET serialization architecture provides three serialization types: the SerializationEntry structure, the Serializationlnfo class, and the StreamingContext structure. You can use these three types to specify values that are required to represent an object in a serialized form and retrieve them appropriately during deserialization. In addition to the values in an object, these serialization types also provide information, such as the type of the object in the serialized stream, corresponding assembly name, and the context in which the current serialization is taking place. These serialization types help you have more control when implementing the custom serialization and deserialization functionality for a certain type.

With serialization types, you can get the information about serialized and deserialized objects before and after serialization and deserialization, respectively.

The following table describes and shows implementation of the SerilaizationEntry structure.



The SerializationEntry structure contains the Name, ObjectType, and Value properties to retrieve the name, type, and value of the serialized object, respectively. Therefore, by using this structure when serializing an object, you can pass the information that is necessary for deserialization.

The following code example shows the retrieval of the Name, Type, and Value of an object stored in the SerializationEntry instance.

Visual Basic

Dim entry As New SerializationEntry

Dim val As String

Dim type as Type

Dim obj as Object val = entry.Name type = entry.ObjectType obj = entry.Value




SerializationEntry entry = new SerializationEntry(); string val; Type type; Object obj; val = entry.Name; type = entry.ObjectType; obj = entry.Value; Console.WriteLine(val); Console.WriteLine(type); Console.WriteLine(obj);

The following table describes and shows implementation of the SerializationInfo class.



The SerializationInfo class belongs to the

System.Runtime.Serialization namespace. This class stores all the data needed to serialize or

The code example shows the addition of two values in the form of name/value pairs to an object of the SerializationInfo class. Visual Basic

deserialize an object. This data includes the values representing the state of the object and the name of the assembly and the object type. The object type is the type of object being serialized. For example, if a Customer object is the target of the serialization process, the object type indicates Customer.

Private Sub GetObjectData(ByVal info

SerializationInfo, _

ByVal context As Streami ngContext)

info.AddValue("Name", "John") info.AddValue("Location", "London") End Sub

The SerializationInfo class can be considered as a store of various


values that represent the state of an object. These values are stored in the form of name/value pairs. This class cannot be inherited. The SerializationInfo class is a collection of SerializationEntry structures. You can use the SerializationInfo class when

private void GetObjectData(SerializationInfo info, StreamingContext context)

info.AddValue("Name", "John"); info.AddValue("Location",

"London"); }

implementing custom serialization behavior in a certain type.

The following table describes and shows implementation of the StreamingContext structure.



The StreamingContext structure provides information about the source and destination of a specific serialized stream. This helps you provide customized functionality for serialization and deserialization of the corresponding object in a stream, depending on the context of the source or destination of the stream. This structure also provides an additional caller-defined context. This context can help you connect objects together. It is the calling method defined as the caller that specifies the context of an object when this method creates the object.

The code example shows the retrieval of the state of an object by using the StreamingContext structure.

Visual Basic

Dim instance As New StreamingContext Dim value As StreamingContextStates value = instance.State

StreamingContext instance = new Streami ngContext();

StreamingContextStates value; value = instance.State;


Note: Refer to the . job aid describing the members of the SerializationEntry structure, the SerializationInfo class, and the StreamingContext structure.

Was this article helpful?

0 0

Post a comment