Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All

BinaryFormatter.Deserialize Method (Stream)

Deserializes the specified stream into an object graph.

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

'Declaration
Public Function Deserialize ( _
	serializationStream As Stream _
) As Object

Parameters

serializationStream
Type: System.IO.Stream

The stream from which to deserialize the object graph.

Return Value

Type: System.Object
The top (root) of the object graph.

Implements

IFormatter.Deserialize(Stream)

ExceptionCondition
ArgumentNullException

The serializationStream is Nothing.

SerializationException

The serializationStream supports seeking, but its length is 0.

-or-

The target type is a Decimal, but the value is out of range of the Decimal type.

SecurityException

The caller does not have the required permission.

For successful deserialization, the current position in the stream must be at the beginning of the object graph.

Security noteSecurity Note

Calling this method with untrusted data is a security risk. Call this method only with trusted data. For more information, see Untrusted Data Security Risks.

Imports System.IO
Imports System.Collections
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization


Module App

    Sub Main()
        Serialize()
        Deserialize()
    End Sub 

    Sub Serialize()

        ' Create a hashtable of values that will eventually be serialized. 
        Dim addresses As New Hashtable
        addresses.Add("Jeff", "123 Main Street, Redmond, WA 98052")
        addresses.Add("Fred", "987 Pine Road, Phila., PA 19116")
        addresses.Add("Mary", "PO Box 112233, Palo Alto, CA 94301")

        ' To serialize the hashtable (and its key/value pairs),   
        ' you must first open a stream for writing.  
        ' In this case, use a file stream. 
        Dim fs As New FileStream("DataFile.dat", FileMode.Create)

        ' Construct a BinaryFormatter and use it to serialize the data to the stream. 
        Dim formatter As New BinaryFormatter
        Try
            formatter.Serialize(fs, addresses)
        Catch e As SerializationException
            Console.WriteLine("Failed to serialize. Reason: " & e.Message)
            Throw 
        Finally
            fs.Close()
        End Try 
    End Sub 



    Sub Deserialize()
        ' Declare the hashtable reference. 
        Dim addresses As Hashtable = Nothing 

        ' Open the file containing the data that you want to deserialize. 
        Dim fs As New FileStream("DataFile.dat", FileMode.Open)
        Try 
            Dim formatter As New BinaryFormatter

            ' Deserialize the hashtable from the file and  
            ' assign the reference to the local variable.
            addresses = DirectCast(formatter.Deserialize(fs), Hashtable)
        Catch e As SerializationException
            Console.WriteLine("Failed to deserialize. Reason: " & e.Message)
            Throw 
        Finally
            fs.Close()
        End Try 

        ' To prove that the table deserialized correctly,  
        ' display the key/value pairs. 
        Dim de As DictionaryEntry
        For Each de In addresses
            Console.WriteLine("{0} lives at {1}.", de.Key, de.Value)
        Next 
    End Sub 
End Module

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0, 1.1

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Show:
© 2015 Microsoft