IDataContractSurrogate.GetDeserializedObject(Object, Type) Méthode

Définition

Pendant la désérialisation, retourne un objet qui est un substitut pour l’objet spécifié.

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

Paramètres

obj
Object

Objet désérialisé à substituer.

targetType
Type

Type auquel l'objet substitué doit être assigné.

Retours

Objet désérialisé substitué. Cet objet doit être d'un type qui est sérialisable par le DataContractSerializer. Par exemple, il doit être marqué avec l'attribut DataContractAttribute ou d'autres mécanismes que le sérialiseur reconnaît.

Exemples

L'exemple suivant illustre une implémentation de la méthode 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

Remarques

Dans une implémentation simple, utilisez une structure de contrôle if…then…else pour tester si la valeur obj est du type substitué. Si tel est le cas, transformez-le en conséquence et retournez l'objet substitué. L'objet substitué peut être une nouvelle instance ou la même instance obj.

S’applique à