FormatterServices.GetObjectData Method (Object, MemberInfo())


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 Shared Function GetObjectData (
	obj As Object,
	members As MemberInfo()
) As Object()


Type: System.Object

The object to write to the formatter.

Type: System.Reflection.MemberInfo()

The members to extract from the object.

Return Value

Type: System.Object()

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

Exception Condition

The obj or members parameter is null.

An element of members is null.


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.

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()
        Catch exc As System.Exception
            Console.WriteLine("{0}: {1}", _
            exc.Message, exc.StackTrace)
            Console.WriteLine("Press <Enter> to exit....")
        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() = _

        ' 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


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

.NET Framework
Available since 1.1
Return to top