SurrogateSelector Classe

Définition

Assiste les formateurs lors de la sélection du substitut de sérialisation, afin de lui déléguer le processus de sérialisation ou de désérialisation.

public ref class SurrogateSelector : System::Runtime::Serialization::ISurrogateSelector
public class SurrogateSelector : System.Runtime.Serialization.ISurrogateSelector
[System.Runtime.InteropServices.ComVisible(true)]
public class SurrogateSelector : System.Runtime.Serialization.ISurrogateSelector
type SurrogateSelector = class
    interface ISurrogateSelector
[<System.Runtime.InteropServices.ComVisible(true)>]
type SurrogateSelector = class
    interface ISurrogateSelector
Public Class SurrogateSelector
Implements ISurrogateSelector
Héritage
SurrogateSelector
Dérivé
Attributs
Implémente

Exemples

L’exemple de code suivant montre comment créer une classe de substitution de sérialisation qui sait comment sérialiser ou désérialiser correctement une classe qui n’est pas elle-même sérialisable. En outre, cet exemple montre également comment récupérer à partir d’un 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 object's name and address fields.
    public void GetObjectData(Object obj,
        SerializationInfo info, StreamingContext context)
    {

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

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

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

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.
            var 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.
                var 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

Remarques

Une substitution de sérialisation donne aux utilisateurs un objet qui peut gérer les exigences de sérialisation d’un autre objet et peut transformer les données sérialisées si nécessaire.

Constructeurs

SurrogateSelector()

Initialise une nouvelle instance de la classe SurrogateSelector.

Méthodes

AddSurrogate(Type, StreamingContext, ISerializationSurrogate)

Ajoute un substitut à la liste des substituts vérifiés.

ChainSelector(ISurrogateSelector)

Ajoute le ISurrogateSelector spécifié et capable de gérer un type d'objet particulier à la liste des substituts.

Equals(Object)

Détermine si l'objet spécifié est égal à l'objet actuel.

(Hérité de Object)
GetHashCode()

Fait office de fonction de hachage par défaut.

(Hérité de Object)
GetNextSelector()

Retourne le sélecteur suivant dans la chaîne de sélecteurs.

GetSurrogate(Type, StreamingContext, ISurrogateSelector)

Retourne le substitut pour un type particulier.

GetType()

Obtient le Type de l'instance actuelle.

(Hérité de Object)
MemberwiseClone()

Crée une copie superficielle du Object actuel.

(Hérité de Object)
RemoveSurrogate(Type, StreamingContext)

Supprime le substitut associé à un type donné.

ToString()

Retourne une chaîne qui représente l'objet actuel.

(Hérité de Object)

S’applique à