FormatterServices.PopulateObjectMembers Method (Object, MemberInfo[], Object[])


The .NET API Reference documentation has a new home. Visit the .NET API Browser on to see the new experience.

Populates the specified object with values for each field drawn from the data array of objects.

Namespace:   System.Runtime.Serialization
Assembly:  mscorlib (in mscorlib.dll)

public static object PopulateObjectMembers(
	object obj,
	MemberInfo[] members,
	object[] data


Type: System.Object

The object to populate.

Type: System.Reflection.MemberInfo[]

An array of MemberInfo that describes which fields and properties to populate.

Type: System.Object[]

An array of Object that specifies the values for each field and property to populate.

Return Value

Type: System.Object

The newly populated object.

Exception Condition

The obj, members, or data parameter is null.

An element of members is null.


The length of members does not match the length of data.


An element of members is not an instance of FieldInfo.


The caller does not have the required permission.

If an element in data is null, PopulateObjectMembers does not write anything to that field.

The following example creates an instance of a Book class and sets field values on the instance. The code then gets the type information using the GetSerializableMembers method. The code copies the instance data into an object array using the GetObjectData method. A new uninitialized instance of the class is created using the GetSafeUninitializedObject method. Finally, the data from the first instance is copied into the second instance using the PopulateObjectMembers method.

using System;
using System.Collections;
using System.Runtime.Serialization;
using System.IO;
using System.Reflection;
using System.Security.Permissions;

[assembly: SecurityPermission(SecurityAction.RequestMinimum)]
namespace Examples
    // The SerializableAttribute specifies that instances of the class 
    // can be serialized by the BinaryFormatter or SoapFormatter.
    class Book
        public string Title;
        public string Author;

        // Constructor for setting new values.
        public Book(string newTitle, string newAuthor)
            Title = newTitle;
            Author = newAuthor;

    public sealed class Test
        public static void Main()
            catch (System.Exception exc)
                Console.WriteLine("{0}: {1}", exc.Message, exc.StackTrace);
                Console.WriteLine("Press <Enter> to exit....");

        static void Run()
            // Create an instance of a Book class 
            // with a title and author.
            Book Book1 = new Book("Book Title 1",
                "Masato Kawai");

            // Store data about the serializable members in a 
            // MemberInfo array. The MemberInfo type holds 
            // only type data, not instance data.
            MemberInfo[] members =

            // Copy the data from the first book into an 
            // array of objects.
            object[] data =
                FormatterServices.GetObjectData(Book1, members);

            // Create an uninitialized instance of the Book class.
            Book Book1Copy =

            // Call the PopuluateObjectMembers to copy the
            // data into the new Book instance.
                (Book1Copy, members, data);

            // Print the data from the copy.
            Console.WriteLine("Title: {0}", Book1Copy.Title);
            Console.WriteLine("Author: {0}", Book1Copy.Author);
        // A private constructor is good practice on
        // a class containing only static methods.
        private Test() { }


For providing serialization services. Associated enumeration: SecurityPermissionFlag.SerializationFormatter, SecurityAction.LinkDemand.

.NET Framework
Available since 1.1
Return to top