SerializationInfo.GetValue(String, Type) SerializationInfo.GetValue(String, Type) SerializationInfo.GetValue(String, Type) Method

정의

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

검색할 값과 관련된 이름입니다.The name associated with the value to retrieve.

type
Type 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.

반환

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 는 요청 된 형식 (또는 해당 파생된 클래스 중 하나), 해당 값이 반환 직접.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.

적용 대상