FormatterServices.PopulateObjectMembers メソッドとは? わかりやすく解説

Weblio 辞書 > コンピュータ > .NET Framework クラス ライブラリ リファレンス > FormatterServices.PopulateObjectMembers メソッドの意味・解説 

FormatterServices.PopulateObjectMembers メソッド

指定したオブジェクトに、オブジェクトデータ配列から抽出された各フィールドの値を設定します

名前空間: System.Runtime.Serialization
アセンブリ: mscorlib (mscorlib.dll 内)
構文構文

Public Shared Function PopulateObjectMembers
 ( _
    obj As Object, _
    members As MemberInfo(), _
    data As Object() _
) As Object
Dim obj As Object
Dim members As MemberInfo()
Dim data As Object()
Dim returnValue As Object

returnValue = FormatterServices.PopulateObjectMembers(obj, members, data)
public static Object PopulateObjectMembers
 (
    Object obj,
    MemberInfo[] members,
    Object[] data
)
public:
static Object^ PopulateObjectMembers (
    Object^ obj, 
    array<MemberInfo^>^ members, 
    array<Object^>^ data
)
public static Object PopulateObjectMembers
 (
    Object obj, 
    MemberInfo[] members, 
    Object[] data
)
public static function PopulateObjectMembers
 (
    obj : Object, 
    members : MemberInfo[], 
    data : Object[]
) : Object

パラメータ

obj
members
data

戻り値
新しく設定されオブジェクト

例外例外
例外種類条件

ArgumentNullException

objmembersdata の各パラメータnull 参照 (Visual Basic では Nothing) です。

members要素null 参照 (Visual Basic では Nothing) です。

ArgumentException

members長さdata長さ一致しません。

SerializationException

members要素が FieldInfo のインスタンスではありません。

SecurityException

呼び出し元に必要なアクセス許可がありません。

解説解説

data要素null 参照 (Visual Basic では Nothing) の場合PopulateObjectMembers はそのフィールドに何も書き込みません。

使用例使用例

Book クラスインスタンス作成し、そのインスタンスフィールド値を設定する例を次に示します。このコードでは、次に GetSerializableMembers メソッド使用して型情報取得しますまた、GetObjectData メソッド使用してインスタンスデータオブジェクト配列コピーします。さらに、GetSafeUninitializedObject メソッド使用して初期化前の状態に戻したクラス新しインスタンス作成します最後にPopulateObjectMembers メソッド使用して1 つ目のインスタンスデータ2 つ目のインスタンスコピーします

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

.NET Framework のセキュリティ.NET Frameworkセキュリティ
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
FormatterServices クラス
FormatterServices メンバ
System.Runtime.Serialization 名前空間


このページでは「.NET Framework クラス ライブラリ リファレンス」からFormatterServices.PopulateObjectMembers メソッドを検索した結果を表示しています。
Weblioに収録されているすべての辞書からFormatterServices.PopulateObjectMembers メソッドを検索する場合は、下記のリンクをクリックしてください。
 全ての辞書からFormatterServices.PopulateObjectMembers メソッド を検索

英和和英テキスト翻訳>> Weblio翻訳
英語⇒日本語日本語⇒英語
  

辞書ショートカット

すべての辞書の索引

「FormatterServices.PopulateObjectMembers メソッド」の関連用語

FormatterServices.PopulateObjectMembers メソッドのお隣キーワード
検索ランキング

   

英語⇒日本語
日本語⇒英語
   



FormatterServices.PopulateObjectMembers メソッドのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

   
日本マイクロソフト株式会社日本マイクロソフト株式会社
© 2025 Microsoft.All rights reserved.

©2025 GRAS Group, Inc.RSS