IDataContractSurrogate.GetDeserializedObject(Object, Type) Método
Definición
Importante
Parte de la información hace referencia a la versión preliminar del producto, que puede haberse modificado sustancialmente antes de lanzar la versión definitiva. Microsoft no otorga ninguna garantía, explícita o implícita, con respecto a la información proporcionada aquí.
Durante la deserialización, devuelve un objeto que es un sustituto 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
Objeto deserializado que se va a sustituir.
Devoluciones
Objeto deserializado sustituido. Este objeto debe ser de un tipo serializable por .DataContractSerializer Por ejemplo, debe marcarse con el DataContractAttribute atributo u otros mecanismos que reconoce el serializador.
Ejemplos
En el ejemplo siguiente se muestra una implementación del 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
Comentarios
En una implementación sencilla, use un if... entonces... else control structure to test if the obj value is of the surrogated type. Si es así, transforme según sea necesario y devuelva el objeto sustituido. El objeto sustituido puede ser una nueva instancia o la misma obj instancia.