Udostępnij przez


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

Zdeserializowany obiekt, który ma zostać zastąpiony.

targetType
Type

Obiekt Type , do którego należy przypisać obiekt zastępczy.

Zwraca

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

Przykłady

W poniższym przykładzie pokazano 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 elementu if... Następnie... else struktura kontrolki do testowania obj , czy wartość jest typu zastępczego. Jeśli tak, przekształć go w razie potrzeby i zwrócić obiekt podstawiony. Obiekt zastępczy może być nowym wystąpieniem lub tym samym obj wystąpieniem.

Dotyczy