SerializationInfo.GetValue(String, 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í.
Recupera un valor del SerializationInfo almacén.
public:
System::Object ^ GetValue(System::String ^ name, Type ^ type);
public object? GetValue(string name, Type type);
public object GetValue(string name, Type type);
member this.GetValue : string * Type -> obj
Public Function GetValue (name As String, type As Type) As Object
Parámetros
- name
- String
Nombre asociado al valor que se va a recuperar.
- type
- Type
Type del valor que se va a recuperar. Si el valor almacenado no se puede convertir en este tipo, el sistema iniciará una InvalidCastExceptionexcepción .
Devoluciones
Objeto del objeto especificado Type asociado a name.
Excepciones
name o type es null.
El valor asociado a name no se puede convertir en type.
No se encuentra un elemento con el nombre especificado en la instancia actual.
Ejemplos
En el ejemplo de código siguiente se muestra el uso del GetValue método :
// A serializable LinkedList example. For the full LinkedList implementation
// see the Serialization sample.
[Serializable()]
class LinkedList: ISerializable {
public static void Main() {}
Node m_head = null;
Node m_tail = null;
// Serializes the object.
public void GetObjectData(SerializationInfo info, StreamingContext context){
// Stores the m_head and m_tail references in the SerializationInfo info.
info.AddValue("head", m_head, m_head.GetType());
info.AddValue("tail", m_tail, m_tail.GetType());
}
// Constructor that is called automatically during deserialization.
// Reconstructs the object from the information in SerializationInfo info
private LinkedList(SerializationInfo info, StreamingContext context)
{
Node temp = new Node(0);
// Retrieves the values of Type temp.GetType() from SerializationInfo info
m_head = (Node)info.GetValue("head", temp.GetType());
m_tail = (Node)info.GetValue("tail", temp.GetType());
}
}
' A serializable LinkedList example. For the full LinkedList implementation
' see the Serialization sample.
<Serializable()> Class LinkedList
Implements ISerializable
Public Shared Sub Main()
End Sub
Private m_head As Node = Nothing
Private m_tail As Node = Nothing
' Serializes the object.
Public Sub GetObjectData(info As SerializationInfo, _
context As StreamingContext) Implements ISerializable.GetObjectData
' Stores the m_head and m_tail references in the SerializationInfo info.
info.AddValue("head", m_head, m_head.GetType())
info.AddValue("tail", m_tail, m_tail.GetType())
End Sub
' Constructor that is called automatically during deserialization.
' Reconstructs the object from the information in SerializationInfo info.
Private Sub New(info As SerializationInfo, context As StreamingContext)
Dim temp As New Node(0)
' Retrieves the values of Type temp.GetType() from SerializationInfo info.
m_head = CType(info.GetValue("head", temp.GetType()), Node)
m_tail = CType(info.GetValue("tail", temp.GetType()), Node)
End Sub
End Class
Comentarios
Si los datos almacenados en SerializationInfo es del tipo solicitado (o de una de sus clases derivadas), ese valor se devuelve directamente. De lo contrario, IFormatterConverter.Convert se llama a para convertirlo al tipo adecuado.
El valor devuelto por el GetValue método siempre se puede convertir de forma segura al tipo especificado en el type parámetro .