SerializationInfo SerializationInfo SerializationInfo SerializationInfo Class

定義

儲存序列化或還原序列化物件所需的所有資料。Stores all the data needed to serialize or deserialize an object. 這個類別無法被繼承。This class cannot be inherited.

public ref class SerializationInfo sealed
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class SerializationInfo
type SerializationInfo = class
Public NotInheritable Class SerializationInfo
繼承
SerializationInfoSerializationInfoSerializationInfoSerializationInfo
屬性

範例

下列程式碼範例示範SerializationInfo自訂的序列化和還原序列化的各種不同的值。The following code example demonstrates the SerializationInfo for custom serialization and deserialization of various values.

using namespace System;
using namespace System::IO;
using namespace System::Collections;
using namespace System::Runtime::Serialization::Formatters::Binary;
using namespace System::Runtime::Serialization;

ref class SingletonSerializationHelper;

// There should be only one instance of this type per AppDomain.

[Serializable]
public ref class Singleton sealed: public ISerializable
{
private:

   // This is the one instance of this type.
   static Singleton^ theOneObject = gcnew Singleton;

public:

   // Here are the instance fields.
   String^ someString;
   Int32 someNumber;

private:

   // Private constructor allowing this type to construct the singleton.
   Singleton()
   {
      
      // Do whatever is necessary to initialize the singleton.
      someString = "This is a String* field";
      someNumber = 123;
   }

public:

   // A method returning a reference to the singleton.
   static Singleton^ GetSingleton()
   {
      return theOneObject;
   }

   // A method called when serializing a Singleton.
   [System::Security::Permissions::SecurityPermissionAttribute
   (System::Security::Permissions::SecurityAction::LinkDemand, 
   Flags=System::Security::Permissions::SecurityPermissionFlag::SerializationFormatter)]
   virtual void GetObjectData( SerializationInfo^ info, StreamingContext context )
   {
      // Instead of serializing this Object*, we will  
      // serialize a SingletonSerializationHelp instead.
      info->SetType( SingletonSerializationHelper::typeid );

      // No other values need to be added.
   }

   // NOTE: ISerializable*'s special constructor is NOT necessary 
   // because it's never called
};

[Serializable]
private ref class SingletonSerializationHelper sealed: public IObjectReference
{
public:

   // This Object* has no fields (although it could).
   // GetRealObject is called after this Object* is deserialized
   virtual Object^ GetRealObject( StreamingContext context )
   {
      // When deserialiing this Object*, return a reference to 
      // the singleton Object* instead.
      return Singleton::GetSingleton();
   }
};

[STAThread]
int main()
{
   FileStream^ fs = gcnew FileStream( "DataFile.dat",FileMode::Create );
   try
   {
      // Construct a BinaryFormatter and use it 
      // to serialize the data to the stream.
      BinaryFormatter^ formatter = gcnew BinaryFormatter;

      // Create an array with multiple elements refering to 
      // the one Singleton Object*.
      array<Singleton^>^a1 = {Singleton::GetSingleton(),Singleton::GetSingleton()};

      // This displays S"True".
      Console::WriteLine( "Do both array elements refer to the same Object? {0}", (a1[ 0 ] == a1[ 1 ]) );

      // Serialize the array elements.
      formatter->Serialize( fs, a1 );

      // Deserialize the array elements.
      fs->Position = 0;
      array<Singleton^>^a2 = (array<Singleton^>^)formatter->Deserialize( fs );

      // This displays S"True".
      Console::WriteLine( "Do both array elements refer to the same Object? {0}", (a2[ 0 ] == a2[ 1 ]) );

      // This displays S"True".
      Console::WriteLine( "Do all  array elements refer to the same Object? {0}", (a1[ 0 ] == a2[ 0 ]) );
   }
   catch ( SerializationException^ e ) 
   {
      Console::WriteLine( "Failed to serialize. Reason: {0}", e->Message );
      throw;
   }
   finally
   {
      fs->Close();
   }

   return 0;
}
using System;
using System.Text;
using System.IO;
// Add references to Soap and Binary formatters.
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap ;
using System.Runtime.Serialization;


[Serializable]
public class MyItemType : ISerializable
{
    public MyItemType()
    {
        // Empty constructor required to compile.
    }

    // The value to serialize.
    private string myProperty_value;

    public string MyProperty
    {
        get { return myProperty_value; }
        set { myProperty_value = value; }
    }
        
    // Implement this method to serialize data. The method is called 
    // on serialization.
    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        // Use the AddValue method to specify serialized values.
        info.AddValue("props", myProperty_value, typeof(string));

    }

    // The special constructor is used to deserialize values.
    public MyItemType(SerializationInfo info, StreamingContext context)
    {
        // Reset the property value using the GetValue method.
        myProperty_value = (string) info.GetValue("props", typeof(string));
    }
}

// This is a console application. 
public static class Test
{
    static void Main()
    {
        // This is the name of the file holding the data. You can use any file extension you like.
        string fileName = "dataStuff.myData";

        // Use a BinaryFormatter or SoapFormatter.
        IFormatter formatter = new BinaryFormatter();
        //IFormatter formatter = new SoapFormatter();
            
        Test.SerializeItem(fileName, formatter); // Serialize an instance of the class.
        Test.DeserializeItem(fileName, formatter); // Deserialize the instance.
        Console.WriteLine("Done");
        Console.ReadLine();
    }

    public static void SerializeItem(string fileName, IFormatter formatter)
    {
        // Create an instance of the type and serialize it.
        MyItemType t = new MyItemType();
        t.MyProperty = "Hello World";

        FileStream s = new FileStream(fileName , FileMode.Create);
        formatter.Serialize(s, t);            
        s.Close();
    }


    public static void DeserializeItem(string fileName, IFormatter formatter)
    {
        FileStream s = new FileStream(fileName, FileMode.Open);
        MyItemType t = (MyItemType)formatter.Deserialize(s);
        Console.WriteLine(t.MyProperty);            
    }       
}
Imports System.Text
Imports System.IO
' Add references to Soap and Binary formatters.
Imports System.Runtime.Serialization.Formatters.Binary
Imports System.Runtime.Serialization.Formatters.Soap
Imports System.Runtime.Serialization


<Serializable()> _
Public Class MyItemType
    Implements ISerializable
    ' Empty constructor required to compile.
    Public Sub New()
    End Sub

    ' The value to serialize.
    Private myProperty_value As String

    Public Property MyProperty() As String
        Get
            Return myProperty_value
        End Get
        Set(value As String)
            myProperty_value = value
        End Set
    End Property

    ' Implement this method to serialize data. The method is called 
    ' on serialization.
    Public Sub GetObjectData(info As SerializationInfo, context As StreamingContext) Implements ISerializable.GetObjectData
        ' Use the AddValue method to specify serialized values.
        info.AddValue("props", myProperty_value, GetType(String))

    End Sub

    ' The special constructor is used to deserialize values.
    Public Sub New(info As SerializationInfo, context As StreamingContext)
        ' Reset the property value using the GetValue method.
        myProperty_value = DirectCast(info.GetValue("props", GetType(String)), String)
    End Sub
End Class

' This is a console application. 
Public Class Test


    Public Shared Sub Main()
        ' This is the name of the file holding the data. You can use any file extension you like.
        Dim fileName As String = "dataStuff.myData"

        ' Use a BinaryFormatter or SoapFormatter.
        Dim formatter As IFormatter = New BinaryFormatter()
        ' Dim formatter As IFormatter = New SoapFormatter()

        Test.SerializeItem(fileName, formatter)
        ' Serialize an instance of the class.
        Test.DeserializeItem(fileName, formatter)
        ' Deserialize the instance.
        Console.WriteLine("Done")
        Console.ReadLine()
    End Sub

    Public Shared Sub SerializeItem(fileName As String, formatter As IFormatter)
        ' Create an instance of the type and serialize it.
        Dim myType As New MyItemType()
        myType.MyProperty = "Hello World"

        Dim fs As New FileStream(fileName, FileMode.Create)
        formatter.Serialize(fs, myType)
        fs.Close()
    End Sub


    Public Shared Sub DeserializeItem(fileName As String, formatter As IFormatter)
        Dim fs As New FileStream(fileName, FileMode.Open)

        Dim myType As MyItemType = DirectCast(formatter.Deserialize(fs), MyItemType)
        Console.WriteLine(myType.MyProperty)
    End Sub
End Class

備註

這個類別會使用自訂序列化行為的物件。This class is used by objects with custom serialization behavior. GetObjectData上的方法ISerializable或是ISerializationSurrogate填入SerializationInfo存放區名稱、 類型和值的每一份要序列化的資訊。The GetObjectData method on either ISerializable or ISerializationSurrogate populates the SerializationInfo store with the name, type, and value of each piece of information it wants to serialize. 還原序列化期間,適當的函式可以擷取這項資訊。During deserialization, the appropriate function can extract this information.

物件會加入至SerializationInfo儲存在序列化時,使用AddValue方法並擷取自SerializationInfo儲存在還原序列化使用GetValue方法。Objects are added to the SerializationInfo store at serialization time using the AddValue methods and extracted from the SerializationInfo store at deserialization using the GetValue methods.

如需有關自訂序列化的詳細資訊,請參閱 < 自訂序列化For more information about customizing serialization, see Custom Serialization.

建構函式

SerializationInfo(Type, IFormatterConverter) SerializationInfo(Type, IFormatterConverter) SerializationInfo(Type, IFormatterConverter) SerializationInfo(Type, IFormatterConverter)

建立 SerializationInfo 類別的新執行個體。Creates a new instance of the SerializationInfo class.

SerializationInfo(Type, IFormatterConverter, Boolean) SerializationInfo(Type, IFormatterConverter, Boolean) SerializationInfo(Type, IFormatterConverter, Boolean) SerializationInfo(Type, IFormatterConverter, Boolean)

初始化 SerializationInfo 類別的新執行個體。Initializes a new instance of the SerializationInfo class.

屬性

AssemblyName AssemblyName AssemblyName AssemblyName

取得或設定僅在序列化期間序列化之型別的組件名稱。Gets or sets the assembly name of the type to serialize during serialization only.

FullTypeName FullTypeName FullTypeName FullTypeName

取得或設定要序列化之 Type 的完整名稱。Gets or sets the full name of the Type to serialize.

IsAssemblyNameSetExplicit IsAssemblyNameSetExplicit IsAssemblyNameSetExplicit IsAssemblyNameSetExplicit

取得組件名稱是否已明確設定。Gets whether the assembly name has been explicitly set.

IsFullTypeNameSetExplicit IsFullTypeNameSetExplicit IsFullTypeNameSetExplicit IsFullTypeNameSetExplicit

取得完整型別名稱是否已明確設定。Gets whether the full type name has been explicitly set.

MemberCount MemberCount MemberCount MemberCount

取得已經加入至 SerializationInfo 存放區的成員數目。Gets the number of members that have been added to the SerializationInfo store.

ObjectType ObjectType ObjectType ObjectType

傳回要序列化之物件的型別。Returns the type of the object to be serialized.

方法

AddValue(String, Boolean) AddValue(String, Boolean) AddValue(String, Boolean) AddValue(String, Boolean)

加入布林值 (Boolean) 至 SerializationInfo 存放區。Adds a Boolean value into the SerializationInfo store.

AddValue(String, Byte) AddValue(String, Byte) AddValue(String, Byte) AddValue(String, Byte)

加入 8 位元不帶正負號的整數值 (Unsigned Integer) 至 SerializationInfo 存放區。Adds an 8-bit unsigned integer value into the SerializationInfo store.

AddValue(String, Char) AddValue(String, Char) AddValue(String, Char) AddValue(String, Char)

加入 Unicode 字元值至 SerializationInfo 存放區。Adds a Unicode character value into the SerializationInfo store.

AddValue(String, DateTime) AddValue(String, DateTime) AddValue(String, DateTime) AddValue(String, DateTime)

加入 DateTime 值至 SerializationInfo 存放區。Adds a DateTime value into the SerializationInfo store.

AddValue(String, Decimal) AddValue(String, Decimal) AddValue(String, Decimal) AddValue(String, Decimal)

加入十進位值至 SerializationInfo 存放區。Adds a decimal value into the SerializationInfo store.

AddValue(String, Double) AddValue(String, Double) AddValue(String, Double) AddValue(String, Double)

加入雙精度浮點數值至 SerializationInfo 存放區。Adds a double-precision floating-point value into the SerializationInfo store.

AddValue(String, Int16) AddValue(String, Int16) AddValue(String, Int16) AddValue(String, Int16)

加入 16 位元帶正負號的整數值 (Signed Integer) 至 SerializationInfo 存放區。Adds a 16-bit signed integer value into the SerializationInfo store.

AddValue(String, Int32) AddValue(String, Int32) AddValue(String, Int32) AddValue(String, Int32)

將 32 位元帶正負號的整數值新增至 SerializationInfo 存放區。Adds a 32-bit signed integer value into the SerializationInfo store.

AddValue(String, Int64) AddValue(String, Int64) AddValue(String, Int64) AddValue(String, Int64)

將 64 位元帶正負號的整數值新增至 SerializationInfo 存放區。Adds a 64-bit signed integer value into the SerializationInfo store.

AddValue(String, Object) AddValue(String, Object) AddValue(String, Object) AddValue(String, Object)

加入指定的物件至 SerializationInfo 存放區,該物件與存放區中指定的名稱相關聯。Adds the specified object into the SerializationInfo store, where it is associated with a specified name.

AddValue(String, Object, Type) AddValue(String, Object, Type) AddValue(String, Object, Type) AddValue(String, Object, Type)

將值新增至 SerializationInfo 存放區,其中 valuename 建立關聯,並序列化為 TypetypeAdds a value into the SerializationInfo store, where value is associated with name and is serialized as being of Typetype.

AddValue(String, SByte) AddValue(String, SByte) AddValue(String, SByte) AddValue(String, SByte)

加入 8 位元帶正負號的整數值至 SerializationInfo 存放區。Adds an 8-bit signed integer value into the SerializationInfo store.

AddValue(String, Single) AddValue(String, Single) AddValue(String, Single) AddValue(String, Single)

加入單精確度浮點數值至 SerializationInfo 存放區。Adds a single-precision floating-point value into the SerializationInfo store.

AddValue(String, UInt16) AddValue(String, UInt16) AddValue(String, UInt16) AddValue(String, UInt16)

加入 16 位元不帶正負號的整數值至 SerializationInfo 存放區。Adds a 16-bit unsigned integer value into the SerializationInfo store.

AddValue(String, UInt32) AddValue(String, UInt32) AddValue(String, UInt32) AddValue(String, UInt32)

將 32 位元不帶正負號的整數值新增至 SerializationInfo 存放區。Adds a 32-bit unsigned integer value into the SerializationInfo store.

AddValue(String, UInt64) AddValue(String, UInt64) AddValue(String, UInt64) AddValue(String, UInt64)

將 64 位元不帶正負號的整數值新增至 SerializationInfo 存放區。Adds a 64-bit unsigned integer value into the SerializationInfo store.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetBoolean(String) GetBoolean(String) GetBoolean(String) GetBoolean(String)

SerializationInfo 存放區擷取布林值。Retrieves a Boolean value from the SerializationInfo store.

GetByte(String) GetByte(String) GetByte(String) GetByte(String)

SerializationInfo 存放區擷取 8 位元不帶正負號的整數值。Retrieves an 8-bit unsigned integer value from the SerializationInfo store.

GetChar(String) GetChar(String) GetChar(String) GetChar(String)

SerializationInfo 存放區擷取 Unicode 字元值。Retrieves a Unicode character value from the SerializationInfo store.

GetDateTime(String) GetDateTime(String) GetDateTime(String) GetDateTime(String)

DateTime 存放區擷取 SerializationInfo 值。Retrieves a DateTime value from the SerializationInfo store.

GetDecimal(String) GetDecimal(String) GetDecimal(String) GetDecimal(String)

SerializationInfo 存放區擷取十進位值。Retrieves a decimal value from the SerializationInfo store.

GetDouble(String) GetDouble(String) GetDouble(String) GetDouble(String)

SerializationInfo 存放區擷取雙精度浮點數值。Retrieves a double-precision floating-point value from the SerializationInfo store.

GetEnumerator() GetEnumerator() GetEnumerator() GetEnumerator()

傳回 SerializationInfoEnumerator,可用於逐一查看 SerializationInfo 存放區中的名稱 / 值組。Returns a SerializationInfoEnumerator used to iterate through the name-value pairs in the SerializationInfo store.

GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetInt16(String) GetInt16(String) GetInt16(String) GetInt16(String)

SerializationInfo 存放區擷取 16 位元帶正負號的整數值。Retrieves a 16-bit signed integer value from the SerializationInfo store.

GetInt32(String) GetInt32(String) GetInt32(String) GetInt32(String)

SerializationInfo 存放區擷取 32 位元帶正負號的整數值。Retrieves a 32-bit signed integer value from the SerializationInfo store.

GetInt64(String) GetInt64(String) GetInt64(String) GetInt64(String)

SerializationInfo 存放區擷取 64 位元帶正負號的整數值。Retrieves a 64-bit signed integer value from the SerializationInfo store.

GetSByte(String) GetSByte(String) GetSByte(String) GetSByte(String)

SerializationInfo 存放區擷取 8 位元帶正負號的整數值。Retrieves an 8-bit signed integer value from the SerializationInfo store.

GetSingle(String) GetSingle(String) GetSingle(String) GetSingle(String)

SerializationInfo 存放區擷取單精確度浮點數值。Retrieves a single-precision floating-point value from the SerializationInfo store.

GetString(String) GetString(String) GetString(String) GetString(String)

String 存放區擷取 SerializationInfo 值。Retrieves a String value from the SerializationInfo store.

GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
GetUInt16(String) GetUInt16(String) GetUInt16(String) GetUInt16(String)

SerializationInfo 存放區擷取 16 位元不帶正負號的整數值。Retrieves a 16-bit unsigned integer value from the SerializationInfo store.

GetUInt32(String) GetUInt32(String) GetUInt32(String) GetUInt32(String)

SerializationInfo 存放區擷取 32 位元不帶正負號的整數值。Retrieves a 32-bit unsigned integer value from the SerializationInfo store.

GetUInt64(String) GetUInt64(String) GetUInt64(String) GetUInt64(String)

SerializationInfo 存放區擷取 64 位元不帶正負號的整數值。Retrieves a 64-bit unsigned integer value from the SerializationInfo store.

GetValue(String, Type) GetValue(String, Type) GetValue(String, Type) GetValue(String, Type)

SerializationInfo 存放區擷取值。Retrieves a value from the SerializationInfo store.

MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
SetType(Type) SetType(Type) SetType(Type) SetType(Type)

設定要序列化之物件的 TypeSets the Type of the object to serialize.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

適用於

另請參閱