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 名前空間



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

辞書ショートカット

すべての辞書の索引

「SurrogateSelector」の関連用語

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

   

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



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

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

©2025 GRAS Group, Inc.RSS