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

Definition

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

public:
 static System::Object ^ PopulateObjectMembers(System::Object ^ obj, cli::array <System::Reflection::MemberInfo ^> ^ members, cli::array <System::Object ^> ^ data);
[System.Security.SecurityCritical]
public static object PopulateObjectMembers (object obj, System.Reflection.MemberInfo[] members, object[] data);
static member PopulateObjectMembers : obj * System.Reflection.MemberInfo[] * obj[] -> obj
Public Shared Function PopulateObjectMembers (obj As Object, members As MemberInfo(), data As Object()) As Object

Parameters

obj
Object Object Object Object

The object to populate.

members
MemberInfo[]

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

data
Object[]

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

Returns

The newly populated object.

Exceptions

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.

The caller does not have the required permission.

Examples

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.
    [Serializable]
    class Book
    {
        public string Title;
        public string Author;

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

    [SecurityPermission(SecurityAction.Demand)]
    public sealed class Test
    {
        public static void Main()
        {
            try
            {
                Run();
            }
            catch (System.Exception exc)
            {
                Console.WriteLine("{0}: {1}", exc.Message, exc.StackTrace);
            }
            finally
            {
                Console.WriteLine("Press <Enter> to exit....");
                Console.ReadLine();
            }
        }

        
        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 =
               FormatterServices.GetSerializableMembers
               (typeof(Book));

            // 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 =
                (Book)FormatterServices.GetSafeUninitializedObject
                (typeof(Book));

            // Call the PopuluateObjectMembers to copy the
            // data into the new Book instance.
            FormatterServices.PopulateObjectMembers
                (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() { }
    }
}
Imports System
Imports System.Collections
Imports System.Runtime.Serialization
Imports System.IO
Imports System.Reflection
Imports System.Security.Permissions

<Assembly: SecurityPermission(SecurityAction.RequestMinimum)> 

' The SerializableAttribute specifies that instances of the class 
' can be serialized by the BinaryFormatter or SoapFormatter.
<Serializable()> _
Class Book
    Public Title As String
    Public Author As String

    ' Constructor for setting new values.
    Public Sub New(ByVal newTitle As String, _
    ByVal newAuthor As String)
        Title = newTitle
        Author = newAuthor

    End Sub
End Class

<SecurityPermission(SecurityAction.Demand)> _
               Public NotInheritable Class Test

    Public Shared Sub Main()
        Try
            Run()
        Catch exc As System.Exception
            Console.WriteLine("{0}: {1}", _
            exc.Message, exc.StackTrace)
        Finally
            Console.WriteLine("Press <Enter> to exit....")
            Console.ReadLine()
        End Try

    End Sub


    Shared Sub Run()
        ' Create an instance of a Book class 
        ' with a title and author.
        Dim Book1 As 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.
        Dim members As MemberInfo() = _
           FormatterServices.GetSerializableMembers(GetType(Book))

        ' Copy the data from the first book into an 
        ' array of objects.
        Dim data As Object() = _
            FormatterServices.GetObjectData(Book1, members)

        ' Create an uninitialized instance of the Book class.
        Dim Book1Copy As Book = _
        CType(FormatterServices.GetSafeUninitializedObject _
           (GetType(Book)), Book)

        ' Call the PopuluateObjectMembers to copy the
        ' data into the new Book instance.
        FormatterServices.PopulateObjectMembers _
        (Book1Copy, members, data)

        ' Print the data from the copy.
        Console.WriteLine("Title: {0}", Book1Copy.Title)
        Console.WriteLine("Author: {0}", Book1Copy.Author)

    End Sub

    ' A private constructor is good practice on
    ' a class containing only static methods.
    Private Sub New()

    End Sub
End Class

Remarks

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

Security

SecurityPermission
For providing serialization services. Associated enumeration: SerializationFormatter, LinkDemand.

Applies to