語言

SerializationInfo.GetValue(String, Type) 方法

定義

從儲存庫取得一個數值 SerializationInfo 。

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

參數

name
String

與要取回的值相關聯的名稱。

type
Type

Type取回的價值。 若儲存值無法轉換為此類型,系統會拋出一個 InvalidCastException。

傳回

指定的 Type 物件與 name相關聯。

例外狀況

name 或 type 為 null。

與 name 相關的值無法轉換為 type。

目前實例中找不到指定名稱的元素。

範例

以下程式碼範例展示了此 GetValue 方法的使用:

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

備註

如果 中 SerializationInfo 儲存的資料屬於請求的類型(或其衍生類別之一),該值會直接回傳。 否則, IFormatterConverter.Convert 會被呼叫來轉換成適當的型別。

方法回傳 GetValue 的值可以安全地鑄造為參數中 type 指定的型別。

適用於