ISerializable ISerializable ISerializable ISerializable Interface

定義

允許物件控制它自己的序列化 (Serialization) 和還原序列化 (Deserialization)。Allows an object to control its own serialization and deserialization.

public interface class ISerializable
[System.Runtime.InteropServices.ComVisible(true)]
public interface ISerializable
type ISerializable = interface
Public Interface ISerializable
衍生
屬性

範例

下列程式碼範例示範使用ISerializable介面來定義類別的自訂序列化行為。The following code example demonstrates the use of the ISerializable interface to define custom serialization behavior for a class.

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

備註

可能會序列化任何類別必須標記為SerializableAttributeAny class that might be serialized must be marked with the SerializableAttribute. 如果類別需要控制其序列化程序,它可以實作ISerializable介面。If a class needs to control its serialization process, it can implement the ISerializable interface. Formatter呼叫GetObjectData在序列化時,並填入所提供SerializationInfo代表的物件所需的所有資料。The Formatter calls the GetObjectData at serialization time and populates the supplied SerializationInfo with all the data required to represent the object. Formatter建立SerializationInfo圖形中物件的型別。The Formatter creates a SerializationInfo with the type of the object in the graph. 需要傳送 proxy 本身可以使用的物件FullTypeNameAssemblyName上的方法SerializationInfo變更傳輸的資訊。Objects that need to send proxies for themselves can use the FullTypeName and AssemblyName methods on SerializationInfo to change the transmitted information.

在類別繼承的情況下是可序列化的類別,衍生自基底類別實作ISerializableIn the case of class inheritance, it is possible to serialize a class that derives from a base class that implements ISerializable. 在此情況下,衍生的類別應該呼叫基底類別實作GetObjectData其實作內部GetObjectDataIn this case, the derived class should call the base class implementation of GetObjectData inside its implementation of GetObjectData. 否則,不會序列化的基底類別中的資料。Otherwise, the data from the base class will not be serialized.

ISerializable介面隱含的建構函式簽章的建構函式 (SerializationInfo的詳細資訊,StreamingContext內容)。The ISerializable interface implies a constructor with the signature constructor (SerializationInfo information, StreamingContext context). 在還原序列化時,目前的建構函式呼叫中的資料後才SerializationInfo已還原序列化格式器。At deserialization time, the current constructor is called only after the data in the SerializationInfo has been deserialized by the formatter. 一般情況下,這個建構函式應受到保護,如果類別並未密封。In general, this constructor should be protected if the class is not sealed.

無法保證還原序列化物件的順序。The order in which objects are deserialized cannot be guaranteed. 例如,如果一種類型會參考具有不尚未還原序列化的型別,就會發生例外狀況。For example, if one type references a type that has not been deserialized yet, an exception will occur. 如果您要建立具有這類相依性的類型,可解決此問題,實作IDeserializationCallback介面和OnDeserialization方法。If you are creating types that have such dependencies, you can work around the problem by implementing the IDeserializationCallback interface and the OnDeserialization method.

序列化架構會處理擴充的物件型別MarshalByRefObject做為擴充的型別相同ObjectThe serialization architecture handles object types that extend MarshalByRefObject the same as types that extend Object. 這些類型都可以使用標記SerializableAttribute並實作ISerializable介面做為其他類型的物件。These types can be marked with the SerializableAttribute and implement the ISerializable interface as any other object type. 將擷取其狀態的物件,並將它保存到資料流中。Their object state will be captured and persisted onto the stream.

當透過使用這些型別System.Runtime.Remoting,遠端基礎結構提供超越一般序列化,並改為將序列化的 proxy 代理MarshalByRefObjectWhen these types are being used through System.Runtime.Remoting, the remoting infrastructure provides a surrogate that preempts typical serialization and instead serializes a proxy to the MarshalByRefObject. 代理是知道如何序列化和還原序列化特定型別之物件的 helper。A surrogate is a helper that knows how to serialize and deserialize objects of a particular type. Proxy,看不到在大部分情況下,使用者都屬於類型ObjRefThe proxy, invisible to the user in most cases, will be of type ObjRef.

一般設計模式,它會兩者都標記為可序列化的屬性和擴充類別不尋常MarshalByRefObjectAs a general design pattern, it would be unusual for a class to be both marked with the serializable attribute and extend MarshalByRefObject. 開發人員應該仔細考量可能的序列化和遠端處理案例結合這兩個特性。Developers should think carefully about the possible serialization and remoting scenarios when combining these two characteristics. 其中,這可能是適用的其中一個範例是使用MemoryStreamOne example where this might be applicable is with a MemoryStream. 雖然的基底類別MemoryStream(Stream) 始MarshalByRefObject,就可以擷取的狀態MemoryStream並會在還原資料庫。While the base class of MemoryStream (Stream) extends from MarshalByRefObject, it is possible to capture the state of a MemoryStream and restore it at will. 因此,它可能會有意義序列化資料庫上的這個資料流的狀態,並在稍後還原的時間。It might, therefore, be meaningful to serialize the state of this stream into a database and restore it at some later point in time. 不過,使用遠端執行功能,此類型的物件會透過 proxy 傳遞。However, when used through remoting, an object of this type would be proxied.

如需有關擴充的類別的序列化MarshalByRefObject,請參閱RemotingSurrogateSelectorFor more information about serialization of classes that extend MarshalByRefObject, see RemotingSurrogateSelector. 如需實作的詳細資訊ISerializable,請參閱 < 自訂序列化For more information about implementing ISerializable, see Custom Serialization.

給實施者的注意事項

實作這個介面,讓要參與自己的序列化和還原序列化的物件。Implement this interface to allow an object to take part in its own serialization and deserialization.

方法

GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext) GetObjectData(SerializationInfo, StreamingContext)

將序列化目標物件所需的資料填入 SerializationInfoPopulates a SerializationInfo with the data needed to serialize the target object.

適用於

另請參閱