Export (0) Print
Expand All

FormatterServices.GetObjectData Method

Extracts the data from the specified object and returns it as an array of objects.

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

public static Object[] GetObjectData (
	Object obj,
	MemberInfo[] members
)
public static Object[] GetObjectData (
	Object obj, 
	MemberInfo[] members
)
public static function GetObjectData (
	obj : Object, 
	members : MemberInfo[]
) : Object[]
Not applicable.

Parameters

obj

The object to write to the formatter.

members

The members to extract from the object.

Return Value

An array of Object that contains data stored in members and associated with obj.

Exception typeCondition

ArgumentNullException

The obj or members parameter is a null reference (Nothing in Visual Basic).

An element of members is a null reference (Nothing in Visual Basic).

SerializationException

An element of members does not represent a field.

For each supplied member of the members array the GetObjectData method extracts the value associated with the obj object, and returns it. The length of the returned array is the same as the length of the members array.

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() { }
    }
}

Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0, 1.1, 1.0

Community Additions

ADD
Show:
© 2015 Microsoft