Export (0) Print
Expand All

SerializationInfo.GetValue Method

Retrieves a value from the SerializationInfo.

[Visual Basic]
Public Function GetValue( _
   ByVal name As String, _
   ByVal type As Type _
) As Object
[C#]
public object GetValue(
 string name,
 Type type
);
[C++]
public: Object* GetValue(
 String* name,
 Type* type
);
[JScript]
public function GetValue(
   name : String,
 type : Type
) : Object;

Parameters

name
The name of the value to retrieve.
type
The type of the value to retrieve. If the stored value cannot be converted to this type, the system will throw an InvalidCastException.

Return Value

The object of the specified Type associated with name.

Exceptions

Exception Type Condition
ArgumentNullException name or type is a null reference (Nothing in Visual Basic).
InvalidCastException The value associated with name cannot be converted to type.
SerializationException An element with the specified name is not found in the current instance.

Remarks

If the data stored in the SerializationInfo is of the type requested (or one of its derived classes), that value is returned directly. Otherwise, IFormatterConverter.Convert is called to convert it to the appropriate type.

Note   The value returned by the GetValue method can always be safely cast to the type specified in the type parameter.

Example

[Visual Basic, C#, C++] The following code example demonstrates the use of the GetValue method:

[Visual Basic] 
' A serializable LinkedList example.  For the full LinkedList implementation
' see the Serialization sample in the .NET Framework SDK.
<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

[C#] 
// A serializable LinkedList example.  For the full LinkedList implementation
// see the Serialization sample in the .NET Framework SDK.
[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());
   }
}

[C++] 
// A serializable LinkedList example.  For the full LinkedList implementation
// see the Serialization sample in the .NET Framework SDK.
[Serializable]
__gc class LinkedList: public ISerializable {

   Node* m_head;
   Node* m_tail;
   
   // Serializes the object.
public:
   void GetObjectData(SerializationInfo* info, StreamingContext /*context*/){
      // Stores the m_head and m_tail references in the SerializationInfo info.
      info->AddValue(S"head", m_head, m_head->GetType());
      info->AddValue(S"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 = dynamic_cast<Node*>(info->GetValue(S"head", temp->GetType()));
      m_tail = dynamic_cast<Node*>(info->GetValue(S"tail", temp->GetType()));
   }
};

[JScript] No example is available for JScript. To view a Visual Basic, C#, or C++ example, click the Language Filter button Language Filter in the upper-left corner of the page.

Requirements

Platforms: Windows 98, Windows NT 4.0, Windows Millennium Edition, Windows 2000, Windows XP Home Edition, Windows XP Professional, Windows Server 2003 family

See Also

SerializationInfo Class | SerializationInfo Members | System.Runtime.Serialization Namespace

Show:
© 2014 Microsoft