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

검색할 값의 Type입니다.The Type of the value to retrieve. 저장된 값을 이 형식으로 변환할 수 없는 경우 시스템은 InvalidCastException을 throw합니다.If the stored value cannot be converted to this type, the system will throw a InvalidCastException.

반환

Type과 관련된 지정된 name의 개체입니다.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에 저장 된 데이터가 요청 된 형식 (또는 해당 파생 클래스 중 하나) 인 경우 해당 값이 직접 반환 됩니다.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.

적용 대상