Microsoft Docs

Windows API Reference

FormatterServices Class

The System.Runtime.Serialization.FormatterServices class provides a set of static methods that can be used by serialization and deserialization processes to assist with the construction of objects, extraction of member data, and other low‑level operations.

Namespace: System.Runtime.Serialization

Assembly: mscorlib.dll

Syntax

public static class FormatterServices

Members

MemberTypeDescription
GetUninitializedObject(Type) Method Creates an instance of the specified type without invoking any constructor.
GetSerializableMembers(Type) Method Retrieves an array of fields that will be serialized for the given type.
GetObjectData(object, FieldInfo[]) Method Extracts the data stored in the fields of an object.
PopulateObjectMembers(object, FieldInfo[], object[]) Method Populates an object’s fields with the supplied data.
IsSerializable(Type) Method Indicates whether a type is marked as serializable.

Example

using System;
using System.Reflection;
using System.Runtime.Serialization;

[Serializable]
public class Person
{
    public string Name;
    public int Age;
    private string Secret = "hidden";
}

class Program
{
    static void Main()
    {
        Person p = new Person { Name = "Alice", Age = 30 };
        // Serialize fields manually
        FieldInfo[] fields = FormatterServices.GetSerializableMembers(typeof(Person));
        object[] data = FormatterServices.GetObjectData(p, fields);
        // Create a new instance without calling constructor
        Person clone = (Person)FormatterServices.GetUninitializedObject(typeof(Person));
        FormatterServices.PopulateObjectMembers(clone, fields, data);
        Console.WriteLine($"{clone.Name}, {clone.Age}");
    }
}

See Also