IDataContractSurrogate.GetDeserializedObject(Object, Type) Método

Definição

Durante a desserialização, devolve um objeto que substitui o 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

O objeto deserializado deve ser substituído.

targetType
Type

O Type que o objeto substituído deve ser atribuído.

Devoluções

O objeto desserializado substituiu. Este objeto deve ser de um tipo que seja serializável pelo DataContractSerializer. Por exemplo, deve ser marcado com o DataContractAttribute atributo ou outros mecanismos que o serializador reconhece.

Exemplos

O exemplo seguinte mostra uma implementação do GetDeserializedObject método.

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

Observações

Numa implementação simples, use um se... Depois... caso contrário, controlar a estrutura para testar se o obj valor é do tipo substituto. Se sim, transforma-o conforme necessário e devolve o objeto substituído. O objeto substituído pode ser uma nova instância ou a mesma obj instância.

Aplica-se a