SerializationInfo.GetValue(String, Type) Método

Definición

Recupera un valor del almacén SerializationInfo.

C#
public object? GetValue (string name, Type type);
C#
public object GetValue (string name, Type type);

Parámetros

name
String

Nombre asociado con el valor que se va a recuperar.

type
Type

Tipo Type del valor que se va a recuperar. Si el valor almacenado no se puede convertir en este tipo, el sistema produce InvalidCastException.

Devoluciones

Objeto del Type especificado que está asociado a name.

Excepciones

name o type es null.

El valor asociado a name no se puede convertir en type.

El elemento con el nombre especificado no se encuentra en la instancia actual.

Ejemplos

En el GetValue ejemplo de código siguiente se muestra el uso del método :

C#
// 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());
   }
}

Comentarios

Si los datos almacenados en SerializationInfo es del tipo solicitado (o una de sus clases derivadas), ese valor se devuelve directamente. De lo contrario, IFormatterConverter.Convert se llama a para convertirlo en el 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 .

Se aplica a

Produto Versións
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9
.NET Framework 1.1, 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0, 2.1