SerializationInfo.GetValue(String, Type) Methode

Definition

Ruft einen Wert aus dem SerializationInfo Speicher ab.

public:
 System::Object ^ GetValue(System::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

Parameter

name
String

Der Name, der dem abzurufenden Wert zugeordnet ist.

type
Type

Der Type abzurufende Wert. Wenn der gespeicherte Wert nicht in diesen Typ konvertiert werden kann, löst das System ein InvalidCastException.

Gibt zurück

Das Objekt des angegebenen Type zugeordneten nameObjekts.

Ausnahmen

name oder type ist null.

Der zugeordnete nametypeWert kann nicht in .

Ein Element mit dem angegebenen Namen wurde in der aktuellen Instanz nicht gefunden.

Beispiele

Im folgenden Codebeispiel wird die Verwendung der GetValue Methode veranschaulicht:

// 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

Hinweise

Wenn die in der SerializationInfo Datei gespeicherten Daten vom angeforderten Typ (oder einer der abgeleiteten Klassen) stammen, wird dieser Wert direkt zurückgegeben. Andernfalls wird sie aufgerufen, IFormatterConverter.Convert um sie in den entsprechenden Typ zu konvertieren.

Der von der GetValue Methode zurückgegebene Wert kann immer sicher in den im type Parameter angegebenen Typ umgewandelt werden.

Gilt für: