IDataContractSurrogate.GetDeserializedObject(Object, Type) Metoda

Definice

Během deserializace vrátí objekt, který je náhradou za zadaný objekt.

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

Parametry

obj
Object

Deserializovaný objekt, který má být nahrazen.

targetType
Type

Objekt Type , ke kterému má být nahrazený objekt přiřazen.

Návraty

Nahrazený deserializovaný objekt. Tento objekt musí být typu, který je serializovatelný objektem DataContractSerializer. Například musí být označen atributem DataContractAttribute nebo jinými mechanismy, které serializátor rozpozná.

Příklady

Následující příklad ukazuje implementaci GetDeserializedObject metody .

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

Poznámky

V jednoduché implementaci použijte if... Pak... else řídicí strukturu k otestování, zda obj je hodnota náhradního typu. Pokud ano, transformujte ho podle potřeby a vraťte nahrazený objekt. Nahrazený objekt může být nová instance nebo stejná obj instance.

Platí pro