SerializationInfoEnumeratorとは? わかりやすく解説

SerializationInfoEnumerator クラス

SerializationInfo のデータ解析するための、フォーマッタ支援の機構提供します。このクラス継承できません。

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

<ComVisibleAttribute(True)> _
Public NotInheritable Class
 SerializationInfoEnumerator
    Implements IEnumerator
Dim instance As SerializationInfoEnumerator
[ComVisibleAttribute(true)] 
public sealed class SerializationInfoEnumerator
 : IEnumerator
[ComVisibleAttribute(true)] 
public ref class SerializationInfoEnumerator
 sealed : IEnumerator
/** @attribute ComVisibleAttribute(true) */ 
public final class SerializationInfoEnumerator
 implements IEnumerator
ComVisibleAttribute(true) 
public final class SerializationInfoEnumerator
 implements IEnumerator
解説解説

現在のクラスは、SerializationInfo格納されている値を解析する標準列挙子です。値を記録する代わりにSerializationInfoEnumerator は、これを作成した SerializationInfoメンバ変数へのポインタ維持します。

このクラスは IEnumerator 機構従います

使用例使用例

ISerializable が基本クラスでは実装されていないけれども、派生クラスでは実装されている場合に、オブジェクト適切にシリアル化または逆シリアル化するために FormatterServices クラス使用する方法次のコード例示します

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;
using System.Reflection;
using System.Security.Permissions;


// Person is a serializable base class.
[Serializable]
public class Person
{
    private String title;

    public Person(String title)
    {
        this.title = title;
    }

    public override String ToString()
    {
        return String.Format("{0}", title);
    }
}

// Employee is a serializable class derived from Person.
[Serializable]
public class Employee : Person
{
    private String title;

    public Employee(String title) : base("Person")
    {
        this.title = title;
    }

    public override String ToString()
    {
        return String.Format("{0} -> {1}", title,
 base.ToString());
    }
}

// Manager is a serializable and ISerializable class derived from Employee.
[Serializable]
public class Manager : Employee, ISerializable
{
    private String title;

    public Manager() : base("Employee")
 
    {
        this.title = "Manager";
    }

    [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
    public void GetObjectData(SerializationInfo
 info, StreamingContext context)
    {

        // Serialize the desired values for this class.
        info.AddValue("title", title);

        // Get the set of serializable members for the class and base
 classes.
        Type thisType = this.GetType();
        MemberInfo[] mi = FormatterServices.GetSerializableMembers(thisType, context);

        // Serialize the base class's fields to the info object.
        for (Int32 i = 0; i < mi.Length; i++)
        {
            // Do not serialize fields for this class.
            if (mi[i].DeclaringType == thisType) continue;

            // Skip this field if it is marked NonSerialized.
            if (Attribute.IsDefined(mi[i], typeof(NonSerializedAttribute)))
 continue;
         
            // Get the value of this field and add it to the SerializationInfo
 object.
            info.AddValue(mi[i].Name, ((FieldInfo) mi[i]).GetValue(this));
        }

        // Call the method below to see the contents of the SerializationInfo
 object.
        DisplaySerializationInfo(info);
    }
 
    private void DisplaySerializationInfo(SerializationInfo
 info)
    {
        SerializationInfoEnumerator e = info.GetEnumerator();
        Console.WriteLine("Values in the SerializationInfo:");
        while (e.MoveNext())
        {
            Console.WriteLine("Name={0}, ObjectType={1}, Value={2}", e.Name,
 e.ObjectType, e.Value);
        }
    }

    [SecurityPermissionAttribute(SecurityAction.Demand, Flags=SecurityPermissionFlag.SerializationFormatter)]
    protected Manager(SerializationInfo info, StreamingContext
 context) : base(null)
    {

        // Get the set of serializable members for the class and base
 classes.
        Type thisType = this.GetType();
        MemberInfo[] mi = FormatterServices.GetSerializableMembers(thisType, context);

        // Deserialize the base class's fields from the info object.
        for (Int32 i = 0; i < mi.Length; i++)
        {
            // Do not deserialize fields for this class.
            if (mi[i].DeclaringType == thisType) continue;

            // For easier coding, treat the member as a FieldInfo object
            FieldInfo fi = (FieldInfo) mi[i];

            // Skip this field if it is marked NonSerialized.
            if (Attribute.IsDefined(mi[i], typeof(NonSerializedAttribute)))
 continue;

            // Get the value of this field from the SerializationInfo
 object.
            fi.SetValue(this, info.GetValue(fi.Name, fi.FieldType));
        }

        // Deserialize the values that were serialized for this class.
        title = info.GetString("title");
    }

    public override String ToString()
    {
        return String.Format("{0} -> {1}", title,
 base.ToString());
    }
}


public sealed class App
{
    public static void Main()
    {
        Run();
    }

    public static void Run()
    {
        using (Stream stream = new MemoryStream())
    {
            IFormatter formatter = new BinaryFormatter();
            Manager m = new Manager();
            Console.WriteLine(m.ToString());
            formatter.Serialize(stream, m);

            stream.Position = 0;
            m = (Manager) formatter.Deserialize(stream);
            Console.WriteLine(m.ToString());
        }
    }
}
// This code produces the following output.
//
//  Manager -> Employee -> Person
//  Values in the SerializaitonInfo:
//  Name=title, ObjectType=System.String, Value=Manager
//  Name=Employee+title, ObjectType=System.String, Value=Employee
//  Name=Person+title, ObjectType=System.String, Value=Person
//  Manager -> Employee -> Person
using namespace System;
using namespace System::IO;
using namespace System::Runtime::Serialization;
using namespace System::Runtime::Serialization::Formatters;
using namespace System::Runtime::Serialization::Formatters::Binary;
using namespace System::Reflection;
using namespace System::Security::Permissions;

// Person is a serializable base class.
[Serializable]
public ref class Person
{
private:
    String^ title;

public:
    Person(String^ title)
    {
        this->title = title;
    }

public:
    virtual String^ ToString() override
    {
        return String::Format("{0}", title);
    }
};

// Employee is a serializable class derived from Person.
[Serializable]
public ref class Employee : public
 Person
{
private:
    String^ title;

public:
    Employee(String^ title) : Person("Person")
    {
        this->title = title;
    }

public:
    virtual String^ ToString() override
    {
        return String::Format("{0} -> {1}", title,
 Person::ToString());
    }
};

// Manager is a serializable and ISerializable class derived from Employee.
[Serializable]
ref class Manager : public Employee, public
 ISerializable
{
private:
    String^ title;

public:
    Manager() : Employee("Employee")
    {
        this->title = "Manager";
    }

public:
    [SecurityPermission(SecurityAction::Demand, SerializationFormatter = true)]
    virtual void GetObjectData(SerializationInfo^ info, StreamingContext
 context)
    {
        // Serialize the desired values for this class.
        info->AddValue("title", title);

        // Get the set of serializable members for the class and base
 classes.
        Type^ thisType = this->GetType();
        array<MemberInfo^>^ serializableMembers =
            FormatterServices::GetSerializableMembers(thisType, context);

        // Serialize the base class's fields to the info object.
        for each (MemberInfo^ serializableMember in
 serializableMembers)
        {
            // Do not serialize fields for this class.
            if (serializableMember->DeclaringType != thisType)
            {
                // Skip this field if it is marked NonSerialized.
                if (!(Attribute::IsDefined(serializableMember
,
                    NonSerializedAttribute::typeid)))
                {
                    // Get the value of this field and add it to the
                    // SerializationInfo object.
                    info->AddValue(serializableMember->Name,
                        ((FieldInfo^)serializableMember)->GetValue(this));
                }
            }
        }

        // Call the method below to see the contents of the
        // SerializationInfo object.
        DisplaySerializationInfo(info);
    }

private:
    static void DisplaySerializationInfo(SerializationInfo^
 info)
    {
        Console::WriteLine("Values in the SerializationInfo:");
        for each (SerializationEntry^ infoEntry in
 info)
        {
            Console::WriteLine("Name={0}, ObjectType={1}, Value={2}",
                infoEntry->Name, infoEntry->ObjectType, infoEntry->Value);
        }
    }

protected:
    Manager(SerializationInfo^ info,
        StreamingContext context) : Employee(nullptr)
    {
        // Get the set of serializable members for the class and base
 classes.
        Type^ thisType = this->GetType();
        array<MemberInfo^>^ serializableMembers =
            FormatterServices::GetSerializableMembers(thisType, context);

        // Deserialize the base class's fields from the info object.
        for each (MemberInfo^ serializableMember in
 serializableMembers)
        {
            // Do not deserialize fields for this class.
            if (serializableMember->DeclaringType != thisType)
            {
                // For easier coding, treat the member as a FieldInfo
 object
                FieldInfo^ fieldInformation = (FieldInfo^)serializableMember;

                // Skip this field if it is marked NonSerialized.
                if (!(Attribute::IsDefined(serializableMember
,
                    NonSerializedAttribute::typeid)))
                {
                    // Get the value of this field from the
                    // SerializationInfo object.
                    fieldInformation->SetValue(this,
                        info->GetValue(fieldInformation->Name,
                        fieldInformation->FieldType));
                }
            }
        }

        // Deserialize the values that were serialized for this class.
        title = info->GetString("title");
    }

public:
    virtual String^ ToString() override
    {
        return String::Format("{0} -> {1}", title,
 Employee::ToString());
    }
};

int main()
{
    Stream^ stream = gcnew MemoryStream();
    IFormatter^ formatter = gcnew BinaryFormatter();
    Manager^ m = gcnew Manager();
    Console::WriteLine(m->ToString());
    formatter->Serialize(stream, m);

    stream->Position = 0;
    m = (Manager^) formatter->Deserialize(stream);
    Console::WriteLine(m->ToString());
}

// This code produces the following output.
//
//  Manager -> Employee -> Person
//  Values in the SerializaitonInfo:
//  Name=title, ObjectType=System.String, Value=Manager
//  Name=Employee+title, ObjectType=System.String, Value=Employee
//  Name=Person+title, ObjectType=System.String, Value=Person
//  Manager -> Employee -> Person
継承階層継承階層
System.Object
  System.Runtime.Serialization.SerializationInfoEnumerator
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバ場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
SerializationInfoEnumerator メンバ
System.Runtime.Serialization 名前空間
IEnumerator
SerializationInfo クラス

SerializationInfoEnumerator プロパティ


パブリック プロパティパブリック プロパティ

  名前 説明
パブリック プロパティ Current 現在調べている項目を取得します
パブリック プロパティ Name 現在調べている項目の名前を取得します
パブリック プロパティ ObjectType 現在調べている項目の型を取得します
パブリック プロパティ Value 現在調べている項目の値を取得します
明示的インターフェイスの実装明示的インターフェイス実装
  名前 説明
インターフェイスの明示的な実装 System.Collections.IEnumerator.Current コレクション内の現在の項目を取得します
参照参照

関連項目

SerializationInfoEnumerator クラス
System.Runtime.Serialization 名前空間
IEnumerator
SerializationInfo クラス

SerializationInfoEnumerator メソッド


SerializationInfoEnumerator メンバ

SerializationInfo のデータ解析するための、フォーマッタ支援の機構提供します。このクラス継承できません。

SerializationInfoEnumerator データ型公開されるメンバを以下の表に示します


パブリック プロパティパブリック プロパティ
  名前 説明
パブリック プロパティ Current 現在調べている項目を取得します
パブリック プロパティ Name 現在調べている項目の名前を取得します
パブリック プロパティ ObjectType 現在調べている項目の型を取得します
パブリック プロパティ Value 現在調べている項目の値を取得します
パブリック メソッドパブリック メソッド
明示的インターフェイスの実装明示的インターフェイス実装
  名前 説明
インターフェイスの明示的な実装 System.Collections.IEnumerator.Current コレクション内の現在の項目を取得します
参照参照

関連項目

SerializationInfoEnumerator クラス
System.Runtime.Serialization 名前空間
IEnumerator
SerializationInfo クラス


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

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

辞書ショートカット

すべての辞書の索引

「SerializationInfoEnumerator」の関連用語

SerializationInfoEnumeratorのお隣キーワード
検索ランキング

   

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



SerializationInfoEnumeratorのページの著作権
Weblio 辞書 情報提供元は 参加元一覧 にて確認できます。

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

©2025 GRAS Group, Inc.RSS