.NET Framework Class Library
SerializationInfo..::.GetValue Method

Retrieves a value from the SerializationInfo store.

Namespace:  System.Runtime.Serialization
Assembly:  mscorlib (in mscorlib.dll)
Syntax

Visual Basic (Declaration)
Public Function GetValue ( _
    name As String, _
    type As Type _
) As Object
Visual Basic (Usage)
Dim instance As SerializationInfo
Dim name As String
Dim type As Type
Dim returnValue As Object

returnValue = instance.GetValue(name, _
    type)
C#
public Object GetValue(
    string name,
    Type type
)
Visual C++
public:
Object^ GetValue(
    String^ name, 
    Type^ type
)
JScript
public function GetValue(
    name : String, 
    type : Type
) : Object

Parameters

name
Type: System..::.String
The name associated with the value to retrieve.
type
Type: System..::.Type
The Type of the value to retrieve. If the stored value cannot be converted to this type, the system will throw a InvalidCastException.

Return Value

Type: System..::.Object
The object of the specified Type associated with name.
Exceptions

ExceptionCondition
ArgumentNullException

name or type is nullNothingnullptra 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.

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

Examples

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.
<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.
[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());
   }
}
Visual C++
// 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() ));
   }
};
CPP_OLD
// A serializable LinkedList example.  For the full LinkedList implementation
// see the Serialization sample.
[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()));
   }
};
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
See Also

Reference

Tags :


Page view tracker