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

SurrogateSelector クラス

シリアル化処理または逆シリアル化処理にデリゲートするシリアル化サロゲートの選択のために、フォーマッタを支援します。

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

<ComVisibleAttribute(True)> _
Public Class SurrogateSelector
    Implements ISurrogateSelector
Dim instance As SurrogateSelector
[ComVisibleAttribute(true)] 
public class SurrogateSelector : ISurrogateSelector
[ComVisibleAttribute(true)] 
public ref class SurrogateSelector : ISurrogateSelector
/** @attribute ComVisibleAttribute(true) */ 
public class SurrogateSelector implements ISurrogateSelector
ComVisibleAttribute(true) 
public class SurrogateSelector implements ISurrogateSelector
解説解説
使用例使用例

それ自体はシリアル化できないクラスを正しくシリアル化または逆シリアル化する方法を知っている、シリアル化サロゲート クラスを作成する方法を次のコード例に示します。さらにこの例では、SerializationException から回復する方法も示します。

using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters;
using System.Runtime.Serialization.Formatters.Binary;


// This class is not serializable.
class Employee 
    {
    public String name, address;

    public Employee(String name, String address) 
    {
        this.name = name;
        this.address = address;
    }
}

// This class can manually serialize an Employee object.
sealed class EmployeeSerializationSurrogate : ISerializationSurrogate
 
{

    // Serialize the Employee object to save the objects name and address
 fields.
    public void GetObjectData(Object obj, 
        SerializationInfo info, StreamingContext context) 
    {

        Employee emp = (Employee) obj;
        info.AddValue("name", emp.name);
        info.AddValue("address", emp.address);
    }

    // Deserialize the Employee object to set the objects name and address
 fields.
    public Object SetObjectData(Object obj,
        SerializationInfo info, StreamingContext context,
        ISurrogateSelector selector) 
    {

        Employee emp = (Employee) obj;
        emp.name = info.GetString("name");
        emp.address = info.GetString("address");
        return null;
    }
}

public sealed class App 
{
    static void Main() 
    {
        // This sample uses the BinaryFormatter.
        IFormatter formatter = new BinaryFormatter();

        // Create a MemoryStream that the object will be serialized
 into and deserialized from.
        using (Stream stream = new MemoryStream())
 
        {
            // Create a SurrogateSelector.
            SurrogateSelector ss = new SurrogateSelector();

            // Tell the SurrogateSelector that Employee objects are
 serialized and deserialized 
            // using the EmployeeSerializationSurrogate object.
            ss.AddSurrogate(typeof(Employee),
            new StreamingContext(StreamingContextStates.All),
            new EmployeeSerializationSurrogate());

            // Associate the SurrogateSelector with the BinaryFormatter.
            formatter.SurrogateSelector = ss;

            try 
            {
                // Serialize an Employee object into the memory stream.
                formatter.Serialize(stream, new Employee("Jeff",
 "1 Microsoft Way"));
            }
            catch (SerializationException e) 
            {
                Console.WriteLine("Serialization failed: {0}", e.Message);
                throw;
            }

            // Rewind the MemoryStream.
            stream.Position = 0;

            try 
            {
                // Deserialize the Employee object from the memory stream.
                Employee emp = (Employee) formatter.Deserialize(stream);

                // Verify that it all worked.
                Console.WriteLine("Name = {0}, Address = {1}", emp.name,
 emp.address);
            }
            catch (SerializationException e) 
            {
                Console.WriteLine("Deserialization failed: {0}", e.Message);
                throw;
            }
        }
    }
}

// This code produces the following output.
//
// Name = Jeff, Address = 1 Microsoft Way
継承階層継承階層
System.Object
  System.Runtime.Serialization.SurrogateSelector
スレッド セーフスレッド セーフ
この型の public static (Visual Basic では Shared) メンバはすべて、スレッド セーフです。インスタンス メンバの場合は、スレッド セーフであるとは限りません。
プラットフォームプラットフォーム
バージョン情報バージョン情報
参照参照
関連項目
SurrogateSelector メンバ
System.Runtime.Serialization 名前空間

SurrogateSelector コンストラクタ


SurrogateSelector メソッド


パブリック メソッドパブリック メソッド

プロテクト メソッドプロテクト メソッド
参照参照

関連項目

SurrogateSelector クラス
System.Runtime.Serialization 名前空間

SurrogateSelector メンバ

シリアル化処理または逆シリアル化処理にデリゲートするシリアル化サロゲートの選択のために、フォーマッタを支援します。

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


パブリック コンストラクタパブリック コンストラクタ
  名前 説明
パブリック メソッド SurrogateSelector SurrogateSelector クラスの新しいインスタンスを初期化します。
パブリック メソッドパブリック メソッド
プロテクト メソッドプロテクト メソッド
参照参照

関連項目

SurrogateSelector クラス
System.Runtime.Serialization 名前空間



英和和英テキスト翻訳

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

辞書ショートカット

すべての辞書の索引

「SurrogateSelector」の関連用語

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

   

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



SurrogateSelectorのページの著作権

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

©2026 GRAS Group, Inc.RSS