NetDataContractSerializerSettings
System.Runtime.Serialization

Provides a set of properties that can be used to configure the behavior of the NetDataContractSerializer.

Constructors

Name Description
NetDataContractSerializerSettings() Initializes a new instance of the NetDataContractSerializerSettings class with default settings.

Properties

Name Type Description
IgnoreExtensionDataObject Boolean Gets or sets a value that indicates whether to ignore data for unknown elements. Defaults to false.
MaxItemsInObjectGraph Int32 Gets or sets the maximum number of items to serialize or deserialize in object graphs. Defaults to Int32.MaxValue.
MaxDepth Int32 Gets or sets the maximum depth of the object graph. Defaults to 0 (unlimited).
UseSimpleDictionaryFormat Boolean Gets or sets a value that indicates whether to serialize dictionaries using a simple format. Defaults to false.
PreserveObjectReferences Boolean Gets or sets a value that indicates whether to preserve references to objects in the graph. Defaults to false.
KnownTypes IEnumerable<Type> Gets a collection of known types that can be serialized or deserialized.
SurrogateSelector ISurrogateSelector Gets or sets an object that provides custom serialization and deserialization logic for specific types.

Usage Example

This example demonstrates how to create an instance of NetDataContractSerializerSettings and configure some of its properties before passing it to the NetDataContractSerializer constructor.

using System;
using System.Runtime.Serialization;
using System.Collections.Generic;

// Define a simple class to serialize
public class Person
{
    public string Name { get; set; }
    public int Age { get; set; }
    public List<string> Hobbies { get; set; }
}

public class Example
{
    public static void Main(string[] args)
    {
        // Create a Person object
        Person person = new Person
        {
            Name = "Jane Doe",
            Age = 30,
            Hobbies = new List<string> { "Reading", "Hiking" }
        };

        // Configure serializer settings
        NetDataContractSerializerSettings settings = new NetDataContractSerializerSettings
        {
            MaxDepth = 10,
            PreserveObjectReferences = true,
            IgnoreExtensionDataObject = true
        };

        // Create the serializer with custom settings
        NetDataContractSerializer serializer = new NetDataContractSerializer(settings);

        // Use a MemoryStream to capture the serialized data
        using (var stream = new System.IO.MemoryStream())
        {
            // Serialize the object
            serializer.WriteObject(stream, person);

            // Reset stream position to the beginning for reading
            stream.Position = 0;

            // Deserialize the object
            Person deserializedPerson = (Person)serializer.ReadObject(stream);

            Console.WriteLine($"Deserialized Person: Name = {deserializedPerson.Name}, Age = {deserializedPerson.Age}");
            Console.WriteLine("Hobbies:");
            foreach (var hobby in deserializedPerson.Hobbies)
            {
                Console.WriteLine($"- {hobby}");
            }
        }
    }
}

Remarks

The NetDataContractSerializer is a powerful serialization mechanism that uses Data Contracts to serialize and deserialize objects. It is particularly useful for scenarios requiring interoperability with systems that use the .NET DataContract format, such as WCF services.

By default, the NetDataContractSerializer serializes and deserializes objects without requiring attributes like [DataContract] or [DataMember]. It infers the contract from the object's type and members.

When PreserveObjectReferences is set to true, the serializer includes information to reconstruct object graphs with shared references and cycles. This can increase the size of the serialized output.

Setting MaxItemsInObjectGraph or MaxDepth to a low value can lead to SerializationException if the object graph exceeds these limits. Choose these values carefully based on your expected object structures.

Requirements

Platform Version
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8
.NET Standard Not applicable (this is a .NET Framework specific class)

Namespace

System.Runtime.Serialization

Assembly

System.Runtime.Serialization.dll