共用方式為


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

適用於