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

定義

SerializationInfo ストアから値を取得します。Retrieves a value from the SerializationInfo store.

public:
 System::Object ^ GetValue(System::String ^ name, Type ^ type);
public object GetValue (string name, Type type);
member this.GetValue : string * Type -> obj

パラメーター

name
String

取得する値に関連付けられた名前。The name associated with the value to retrieve.

type
Type

取得する値の TypeThe Type of the value to retrieve. 格納された値がこの型に変換できない場合は、InvalidCastException がスローされます。If the stored value cannot be converted to this type, the system will throw a InvalidCastException.

戻り値

name に関連付けられた、指定した Type のオブジェクト。The object of the specified Type associated with name.

例外

name または typenull です。name or type is null.

name に関連付けられた値を type に変換できません。The value associated with name cannot be converted to type.

指定した名前の要素が、現在のインスタンス内に見つかりません。An element with the specified name is not found in the current instance.

次のコード例は、 GetValueメソッドの使用方法を示しています。The following code example demonstrates the use of the GetValue method:

// A serializable LinkedList example.  For the full LinkedList implementation
// see the Serialization sample.
[Serializable]
ref class LinkedList: public ISerializable
{
private:
   Node^ m_head;
   Node^ m_tail;

   // Serializes the object.
public:
   virtual 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.
private:
   // Reconstructs the object from the information in SerializationInfo info
   LinkedList( SerializationInfo^ info, StreamingContext /*context*/ )
   {
      Node^ temp = gcnew Node( 0 );
      // Retrieves the values of Type temp.GetType() from SerializationInfo info
      m_head = dynamic_cast<Node^>(info->GetValue( "head", temp->GetType() ));
      m_tail = dynamic_cast<Node^>(info->GetValue( "tail", temp->GetType() ));
   }
};
// 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.
   [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter=true)]
   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
   [SecurityPermissionAttribute(SecurityAction.Demand, SerializationFormatter=true)]
   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データが、要求された型 (またはその派生クラスの1つ) である場合、その値は直接返されます。If the data stored in the SerializationInfo is of the type requested (or one of its derived classes), that value is returned directly. それ以外IFormatterConverter.Convertの場合は、を呼び出して適切な型に変換します。Otherwise, IFormatterConverter.Convert is called to convert it to the appropriate type.

GetValueメソッドによって返される値は、常にtypeパラメーターで指定された型に安全にキャストできます。The value returned by the GetValue method can always be safely cast to the type specified in the type parameter.

適用対象