IDataContractSurrogate.GetDeserializedObject(Object, Type) Metoda

Definicja

Podczas deserializacji zwraca obiekt, który jest substytutem określonego obiektu.

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

Obiekt deserializowany, który ma zostać zastąpiony.

targetType
Type

Element Type , do którego powinien zostać przypisany zastąpiony obiekt.

Zwraca

Zastąpiony deserializowany obiekt. Ten obiekt musi być typem, który można serializować przez DataContractSerializerobiekt . Na przykład musi być oznaczony atrybutem DataContractAttribute lub innymi mechanizmami rozpoznawanymi przez serializator.

Przykłady

W poniższym przykładzie przedstawiono implementację 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

Uwagi

W prostej implementacji użyj parametru if... Następnie... else, struktura sterowania, aby sprawdzić, czy obj wartość jest typu zastępczego. Jeśli tak, przekształć go w razie potrzeby i zwrócić zastąpiony obiekt. Zastąpiony obiekt może być nowym wystąpieniem lub tym samym obj wystąpieniem.

Dotyczy