IDataContractSurrogate.GetDataContractType(Type) 方法

定义

在序列化、反序列化以及架构导入和导出期间,返回一个替换指定类型的数据协定类型。

public:
 Type ^ GetDataContractType(Type ^ type);
public Type GetDataContractType (Type type);
abstract member GetDataContractType : Type -> Type
Public Function GetDataContractType (type As Type) As Type

参数

type
Type

要替换的 CLR 类型 Type

返回

替换 Type 值的 type。 此类型必须可由 DataContractSerializer 序列化。 例如,必须用 DataContractAttribute 属性或序列化程序可识别的其他机制对它进行标记。

示例

下面的示例演示 GetDataContractType 方法的实现。

    public Type GetDataContractType(Type type)
{
        Console.WriteLine("GetDataContractType invoked");
        Console.WriteLine("\t type name: {0}", type.Name);
        // "Person" will be serialized as "PersonSurrogated"
        // This method is called during serialization,
        // deserialization, and schema export.
        if (typeof(Person).IsAssignableFrom(type))
{
Console.WriteLine("\t returning PersonSurrogated");
            return typeof(PersonSurrogated);
        }
        return type;
    }
Public Function GetDataContractType(ByVal type As Type) As Type _
   Implements IDataContractSurrogate.GetDataContractType
    Console.WriteLine("GetDataContractType invoked")
    Console.WriteLine(vbTab & "type name: {0}", type.Name)
    ' "Person" will be serialized as "PersonSurrogated"
    ' This method is called during serialization,
    ' deserialization, and schema export.
    If GetType(Person).IsAssignableFrom(type) Then
        Console.WriteLine(vbTab & "returning PersonSurrogated")
        Return GetType(PersonSurrogated)
    End If
    Return type

End Function

适用于