FormatterServices.GetObjectData(Object, MemberInfo[]) Metoda

Definicja

Wyodrębnia dane z określonego obiektu i zwraca je jako tablicę obiektów.Extracts the data from the specified object and returns it as an array of objects.

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

Parametry

obj
Object

Obiekt do zapisu w programie formatującego.The object to write to the formatter.

members
MemberInfo[]

Elementy członkowskie, które mają zostać wyodrębnione z obiektu.The members to extract from the object.

Zwraca

Object[]

Tablica zawierająca Object dane przechowywane w members i skojarzonych z obj .An array of Object that contains data stored in members and associated with obj.

Atrybuty

Wyjątki

objParametr lub members jest null .The obj or members parameter is null.

Element members jest null .An element of members is null.

Element elementu members nie reprezentuje pola.An element of members does not represent a field.

Przykłady

Poniższy przykład tworzy wystąpienie klasy Book i ustawia wartości pól w wystąpieniu.The following example creates an instance of a Book class and sets field values on the instance. Następnie kod pobiera informacje o typie przy użyciu GetSerializableMembers metody.The code then gets the type information using the GetSerializableMembers method. Kod kopiuje dane wystąpienia do tablicy obiektów przy użyciu GetObjectData metody.The code copies the instance data into an object array using the GetObjectData method. Nowe niezainicjowane wystąpienie klasy zostanie utworzone przy użyciu GetSafeUninitializedObject metody.A new uninitialized instance of the class is created using the GetSafeUninitializedObject method. Na koniec dane z pierwszego wystąpienia są kopiowane do drugiego wystąpienia przy użyciu PopulateObjectMembers metody.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.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

Uwagi

Dla każdego dostarczonego elementu członkowskiego members tablicy GetObjectData metoda wyodrębnia wartość skojarzoną z obj obiektem i zwraca ją.For each supplied member of the members array the GetObjectData method extracts the value associated with the obj object, and returns it. Długość zwróconej tablicy jest taka sama jak długość members tablicy.The length of the returned array is the same as the length of the members array.

Dotyczy