System.Runtime.Serialization.Options

Description

Provides options for controlling serialization behavior within the .NET framework. This class is often used to configure specific aspects of how objects are serialized and deserialized, influencing aspects like member selection, handling of null values, and type information inclusion.

public static class Options

Remarks

The System.Runtime.Serialization.Options class is a static utility class that encapsulates various settings and configurations used by serializers. It allows developers to customize the serialization process without directly modifying the serialized objects or the core serialization engine. Key scenarios include specifying which members of a class should be serialized, how to handle circular references, and whether to include assembly information in the serialized output.

This class is particularly useful in conjunction with attributes like [DataMember] and [IgnoreDataMember] to fine-tune serialization at a granular level.

Example

The following example demonstrates how to set a global option to ignore null values during serialization:


using System.Runtime.Serialization;

// Setting a global option to ignore null members
SerializationOptions.IgnoreNullMembers = true;

// Now, when serializing, any null properties will be omitted.

// Example usage with a custom serializer (hypothetical)
// var serializer = new MyCustomSerializer();
// var serializedData = serializer.Serialize(myObject);
// This serialization will adhere to the IgnoreNullMembers setting.
                    

Members

IgnoreNullMembers
public static bool IgnoreNullMembers { get; set; }

Gets or sets a value indicating whether to ignore members that have null values during serialization.

IncludeTypeInformation
public static bool IncludeTypeInformation { get; set; }

Gets or sets a value indicating whether to include type information in the serialized output. This is often useful for deserialization when the exact type cannot be inferred.

MaxDepth
public static int MaxDepth { get; set; }

Gets or sets the maximum depth for serialization to prevent infinite loops in object graphs. A value of 0 or less typically means no limit.

PreserveReferences
public static bool PreserveReferences { get; set; }

Gets or sets a value indicating whether to preserve object references during serialization. This is crucial for handling circular references correctly.