SerializationInfo.GetValue Method (String, Type)

 

Retrieves a value from the SerializationInfo store.

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

Public Function GetValue (
	name As String,
	type As Type
) As 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.

Exception Condition
ArgumentNullException

name or type is null.

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.

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.

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()> 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

.NET Framework
Available since 1.1
Return to top
Show: