IDataContractSurrogate.GetDeserializedObject(Object, Type) Método

Definición

Durante la deserialización, devuelve un objeto que es un suplente del objeto especificado.

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

Parámetros

obj
Object

El objeto deserializado que se va a sustituir.

targetType
Type

Type al que el objeto sustituido deberá estar asignado.

Devoluciones

El objeto deserializado sustituido. Este objeto debe ser de un tipo que DataContractSerializer pueda serializar. Por ejemplo, se debe marcar con el atributo DataContractAttribute u otros mecanismos que el serializador reconoce.

Ejemplos

En el siguiente ejemplo se muestra una implementación del método 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

Comentarios

En una implementación simple, use una estructura de control if...then...else para probar si el valor obj es del tipo suplente. En ese caso, transfórmelo como corresponda y devuelva el objeto sustituido. El objeto sustituido puede ser una nueva instancia o la misma instancia obj.

Se aplica a