共用方式為


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…then…else 控制結構來測試 obj 值是否為被替代的型別。 如果是的話,請依需要加以轉換並傳回已取代的物件。 已取代的物件可能是新執行個體,或是相同的 obj 執行個體。

適用於