語言

IDataContractSurrogate.GetDeserializedObject(Object, Type) 方法

定義

在反序列化過程中,回傳一個物件,作為指定物件的替代品。

public:
 System::Object ^ GetDeserializedObject(System::Object ^ obj, Type ^ targetType);
public object GetDeserializedObject(object obj, Type targetType);
abstract member GetDeserializedObject : obj * Type -> obj
Public Function GetDeserializedObject (obj As Object, targetType As Type) As Object

參數

obj
Object

要替換的去序列化物件。

targetType
Type

該 Type 替換物件應該被指派到該物件。

傳回

替換的去序列化物件。 此物件必須屬於可序列化的 DataContractSerializer類型。 例如,必須標示 DataContractAttribute 序列器識別的屬性或其他機制。

範例

以下範例展示了該 GetDeserializedObject 方法的實作。

public object GetDeserializedObject(Object obj , Type targetType)
{
    Console.WriteLine("GetDeserializedObject invoked");
    // This method is called on deserialization.
    // If PersonSurrogated is being deserialized...
    if (obj is PersonSurrogated)
        {
            //... use the XmlSerializer to do the actual deserialization.
            PersonSurrogated ps = (PersonSurrogated)obj;
            XmlSerializer xs = new XmlSerializer(typeof(Person));
            return (Person)xs.Deserialize(new StringReader(ps.xmlData));
        }
        return obj;
}
Public Function GetDeserializedObject(ByVal obj As Object, _
    ByVal targetType As Type) As Object Implements _
    IDataContractSurrogate.GetDeserializedObject
    Console.WriteLine("GetDeserializedObject invoked")
    ' This method is called on deserialization.
    ' If PersonSurrogated is being deserialized...
    If TypeOf obj Is PersonSurrogated Then
        Console.WriteLine(vbTab & "returning PersonSurrogated")
        '... use the XmlSerializer to do the actual deserialization.
        Dim ps As PersonSurrogated = CType(obj, PersonSurrogated)
        Dim xs As New XmlSerializer(GetType(Person))
        Return CType(xs.Deserialize(New StringReader(ps.xmlData)), Person)
    End If
    Return obj

End Function

備註

在簡單的實作中,使用一個 if...然後......否則控制結構用來測試值是否 obj 屬於替代型態。 如果是這樣,必要時轉換並回傳替換的物件。 被替換的物件可以是新的實例,也可以是相同的 obj 實例。

適用於