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

线程安全

此类型的任何公共静态(Visual Basic 中的 Shared)成员都是线程安全的,但不保证所有实例成员都是线程安全的。

平台

Windows 98、Windows 2000 SP4、Windows Millennium Edition、Windows Server 2003、Windows XP Media Center Edition、Windows XP Professional x64 Edition、Windows XP SP2、Windows XP Starter Edition

.NET Framework 并不是对每个平台的所有版本都提供支持。有关受支持版本的列表,请参见系统要求

版本信息

.NET Framework

受以下版本支持:2.0、1.1、1.0

请参见

参考

SurrogateSelector 成员
System.Runtime.Serialization 命名空间