Export (0) Print
Expand All

ISerializable.GetObjectData Method

Populates a SerializationInfo with the data needed to serialize the target object.

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

'Declaration
Sub GetObjectData ( _
	info As SerializationInfo, _
	context As StreamingContext _
)

Parameters

info
Type: System.Runtime.Serialization.SerializationInfo

The SerializationInfo to populate with data.

context
Type: System.Runtime.Serialization.StreamingContext

The destination (see StreamingContext) for this serialization.

ExceptionCondition
SecurityException

The caller does not have the required permission.

Any objects that are included in the SerializationInfo are automatically tracked and serialized by the formatter.

Code that calls GetObjectData requires the SecurityPermission for providing serialization services. Associated enumeration: SecurityPermissionFlag.SerializationFormatter.

NoteNote

It is not guaranteed that this method will be called only once per object instance during serialization. Therefore, the method should be implemented in such a way that its behavior will be the same regardless of the number of times it is called.

The following example uses the GetObjectData method to set alternate values for a serialized object. The code uses the AddValue method of the SerializationInfo class to store the alternate values when the object is serialized. Conversely, when the constructor of the Person class is called during deserialization, the alternatve values are retrieved using the GetValue method and reassigned to the object's fields.

Imports System
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization
Imports System.Security.Permissions
Imports System.IO

<Assembly: SecurityPermission _
(SecurityAction.RequestMinimum, Execution:=True)> 

Class Program

    Public Shared Sub Main()
        Try
            Run()
        Catch exc As Exception
            Console.WriteLine("{0}: {1}", exc.Message, exc.StackTrace)
        Finally
            Console.WriteLine("Press Enter to exit....")
            Console.ReadLine()
        End Try 

    End Sub 

    Shared Sub Run()
        Dim binaryFmt As New BinaryFormatter()
        Dim p As New Person()
        p.IdNumber = 1010
        p.Name = "AAAAA" 
        Dim fs As New FileStream("Person.xml", FileMode.OpenOrCreate)
        binaryFmt.Serialize(fs, p)
        fs.Close()
        Console.WriteLine _
        ("Original Name: {0}, Original ID: {1}", p.Name, p.IdNumber)

        ' Deserialize.
        fs = New FileStream("Person.xml", FileMode.OpenOrCreate)
        Dim p2 As Person = CType(binaryFmt.Deserialize(fs), Person)
        Console.WriteLine("New Name: {0}, New ID: {1}", _
        p2.Name, p2.IdNumber)
        fs.Close()
    End Sub 
End Class

<Serializable()> _
Public Class Person
    Implements ISerializable
    Private name_value As String 
    Private ID_value As Integer 

    Public Sub New()

    End Sub 

    Protected Sub New(ByVal info As SerializationInfo, _
    ByVal context As StreamingContext)
        If info Is Nothing Then 
            Throw New System.ArgumentNullException("info")
        End If
        name_value = CStr(info.GetValue("AltName", GetType(String)))
        ID_value = Fix(info.GetValue("AltID", GetType(Integer)))

    End Sub

    <SecurityPermission(SecurityAction.LinkDemand, _
    Flags:=SecurityPermissionFlag.SerializationFormatter)> _
    Public Overridable Sub GetObjectData _
    (ByVal info As SerializationInfo, _
    ByVal context As StreamingContext) _
    Implements ISerializable.GetObjectData
        If info Is Nothing Then 
            Throw New System.ArgumentNullException("info")
        End If
        info.AddValue("AltName", "XXX")
        info.AddValue("AltID", 9999)

    End Sub 

    Public Property Name() As String 
        Get 
            Return name_value
        End Get 
        Set(ByVal value As String)
            name_value = value
        End Set 
    End Property 

    Public Property IdNumber() As Integer 
        Get 
            Return ID_value
        End Get 
        Set(ByVal value As Integer)
            ID_value = value
        End Set 
    End Property 
End Class

.NET Framework

Supported in: 4.5.2, 4.5.1, 4.5, 4, 3.5, 3.0, 2.0, 1.1, 1.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

Show:
© 2014 Microsoft