SurrogateSelector.AddSurrogate(Type, StreamingContext, ISerializationSurrogate) 方法

定义

将代理项添加到已检查代理项的列表中。Adds a surrogate to the list of checked surrogates.

public:
 virtual void AddSurrogate(Type ^ type, System::Runtime::Serialization::StreamingContext context, System::Runtime::Serialization::ISerializationSurrogate ^ surrogate);
public virtual void AddSurrogate (Type type, System.Runtime.Serialization.StreamingContext context, System.Runtime.Serialization.ISerializationSurrogate surrogate);
abstract member AddSurrogate : Type * System.Runtime.Serialization.StreamingContext * System.Runtime.Serialization.ISerializationSurrogate -> unit
override this.AddSurrogate : Type * System.Runtime.Serialization.StreamingContext * System.Runtime.Serialization.ISerializationSurrogate -> unit
Public Overridable Sub AddSurrogate (type As Type, context As StreamingContext, surrogate As ISerializationSurrogate)

参数

type
Type

需要其代理项的 TypeThe Type for which the surrogate is required.

context
StreamingContext

上下文特定的数据。The context-specific data.

surrogate
ISerializationSurrogate

要为此类型调用的代理项。The surrogate to call for this type.

例外

typesurrogate 参数为 nullThe type or surrogate parameter is null.

此类型和上下文的代理项已存在。A surrogate already exists for this type and context.

示例

下面的代码示例演示如何调用 AddSurrogate 方法。The following code example demonstrates calling the AddSurrogate method. 此代码示例是为类提供的更大示例的一部分 SurrogateSelectorThis code example is part of a larger example provided for the SurrogateSelector class.

// 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());

适用于