SerializationInfo.GetValue(String, Type) メソッド

定義

SerializationInfo ストアから値を取得します。

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

パラメーター

name
String

取得する値に関連付けられている名前。

type
Type

取得する値の Type 。 格納された値をこの型に変換できない場合、システムは InvalidCastExceptionをスローします。

返品

nameに関連付けられている指定したTypeのオブジェクト。

例外

name または typenull

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 パラメーターで指定された型に安全にキャストできます。

適用対象