共用方式為


SerializableAttribute 類別

定義

表示可以使用二進位或 XML 序列化來序列化類別。 此類別無法獲得繼承。

public ref class SerializableAttribute sealed : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Struct, Inherited=false)]
public sealed class SerializableAttribute : Attribute
[System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Struct, Inherited=false)]
[System.Runtime.InteropServices.ComVisible(true)]
public sealed class SerializableAttribute : Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Struct, Inherited=false)>]
type SerializableAttribute = class
    inherit Attribute
[<System.AttributeUsage(System.AttributeTargets.Class | System.AttributeTargets.Delegate | System.AttributeTargets.Enum | System.AttributeTargets.Struct, Inherited=false)>]
[<System.Runtime.InteropServices.ComVisible(true)>]
type SerializableAttribute = class
    inherit Attribute
Public NotInheritable Class SerializableAttribute
Inherits Attribute
繼承
SerializableAttribute
屬性

範例

下列範例示範以 SerializableAttribute 屬性標記之物件的 SOAP 序列化。

#using <system.dll>
#using <system.messaging.dll>
#using <System.Runtime.Serialization.Formatters.Soap.dll>

using namespace System;
using namespace System::IO;
using namespace System::Runtime::Serialization::Formatters::Soap;

// A test object that needs to be serialized.

[Serializable]
ref class TestSimpleObject
{
private:
   int member1;
   String^ member2;
   String^ member3;
   double member4;

public:

   // A field that is not serialized.

   [NonSerialized]
   String^ member5;
   TestSimpleObject()
   {
      member1 = 11;
      member2 = "hello";
      member3 = "hello";
      member4 = 3.14159265;
      member5 = "hello world!";
   }

   void Print()
   {
      Console::WriteLine( "member1 = ' {0}'", member1 );
      Console::WriteLine( "member2 = ' {0}'", member2 );
      Console::WriteLine( "member3 = ' {0}'", member3 );
      Console::WriteLine( "member4 = ' {0}'", member4 );
      Console::WriteLine( "member5 = ' {0}'", member5 );
   }

};

int main()
{
   // Creates a new TestSimpleObject object.
   TestSimpleObject^ obj = gcnew TestSimpleObject;
   Console::WriteLine( "Before serialization the Object* contains: " );
   obj->Print();

   // Opens a file and serializes the object into it in binary format.
   Stream^ stream = File::Open( "data.xml", FileMode::Create );
   SoapFormatter^ formatter = gcnew SoapFormatter;

   formatter->Serialize( stream, obj );
   stream->Close();

   // Empties obj.
   obj = nullptr;

   // Opens file S"data.xml" and deserializes the object from it.
   stream = File::Open( "data.xml", FileMode::Open );
   formatter = gcnew SoapFormatter;

   obj = dynamic_cast<TestSimpleObject^>(formatter->Deserialize( stream ));
   stream->Close();
   Console::WriteLine( "" );
   Console::WriteLine( "After deserialization the object contains: " );
   obj->Print();
}
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;

public class Test {
   public static void Main()  {

      // Creates a new TestSimpleObject object.
      TestSimpleObject obj = new TestSimpleObject();

      Console.WriteLine("Before serialization the object contains: ");
      obj.Print();

      // Opens a file and serializes the object into it in binary format.
      Stream stream = File.Open("data.xml", FileMode.Create);
      SoapFormatter formatter = new SoapFormatter();

      formatter.Serialize(stream, obj);
      stream.Close();

      // Empties obj.
      obj = null;

      // Opens file "data.xml" and deserializes the object from it.
      stream = File.Open("data.xml", FileMode.Open);
      formatter = new SoapFormatter();

      obj = (TestSimpleObject)formatter.Deserialize(stream);
      stream.Close();

      Console.WriteLine("");
      Console.WriteLine("After deserialization the object contains: ");
      obj.Print();
   }
}

// A test object that needs to be serialized.
[Serializable()]
public class TestSimpleObject  {

    public int member1;
    public string member2;
    public string member3;
    public double member4;

    // A field that is not serialized.
    [NonSerialized()] public string member5;

    public TestSimpleObject() {

        member1 = 11;
        member2 = "hello";
        member3 = "hello";
        member4 = 3.14159265;
        member5 = "hello world!";
    }

    public void Print() {

        Console.WriteLine("member1 = '{0}'", member1);
        Console.WriteLine("member2 = '{0}'", member2);
        Console.WriteLine("member3 = '{0}'", member3);
        Console.WriteLine("member4 = '{0}'", member4);
        Console.WriteLine("member5 = '{0}'", member5);
    }
}
open System
open System.IO
open System.Runtime.Serialization.Formatters.Soap

// A test object that needs to be serialized.
[<Serializable>]
type TestSimpleObject() =
    let member1 = 11
    let member2 = "hello"
    let member3 = "hello"
    let member4 = 3.14159265

    // A field that is not serialized.
    [<NonSerialized>]
    let member5 = "hello world!"

    member _.Print() =
        printfn $"member1 = '{member1}'"
        printfn $"member2 = '{member2}'"
        printfn $"member3 = '{member3}'"
        printfn $"member4 = '{member4}'"
        printfn $"member5 = '{member5}'"

[<EntryPoint>]
let main _ =
    // Creates a new TestSimpleObject object.
    let obj = TestSimpleObject()

    printfn "Before serialization the object contains: "
    obj.Print()

    // Opens a file and serializes the object into it in binary format.
    let stream = File.Open("data.xml", FileMode.Create)
    let formatter = SoapFormatter()

    formatter.Serialize(stream, obj)
    stream.Close()

    // Opens file "data.xml" and deserializes the object from it.
    let stream = File.Open("data.xml", FileMode.Open)
    let formatter = new SoapFormatter()

    let obj = formatter.Deserialize stream :?> TestSimpleObject
    stream.Close()

    printfn "\nAfter deserialization the object contains: "
    obj.Print()
    0
Imports System.IO
Imports System.Runtime.Serialization
Imports System.Runtime.Serialization.Formatters.Soap




Public Class Test
   
   Public Shared Sub Main()
      
      ' Creates a new TestSimpleObject object.
      Dim obj As New TestSimpleObject()
      
      Console.WriteLine("Before serialization the object contains: ")
      obj.Print()
      
      ' Opens a file and serializes the object into it in binary format.
      Dim stream As Stream = File.Open("data.xml", FileMode.Create)
      Dim formatter As New SoapFormatter()
      


      formatter.Serialize(stream, obj)
      stream.Close()
      
      ' Empties obj.
      obj = Nothing
      
      ' Opens file "data.xml" and deserializes the object from it.
      stream = File.Open("data.xml", FileMode.Open)
      formatter = New SoapFormatter()



      obj = CType(formatter.Deserialize(stream), TestSimpleObject)
      stream.Close()
      
      Console.WriteLine("")
      Console.WriteLine("After deserialization the object contains: ")
      obj.Print()

   End Sub

End Class


' A test object that needs to be serialized.
<Serializable()> Public Class TestSimpleObject
   
   Public member1 As Integer
   Public member2 As String
   Public member3 As String
   Public member4 As Double
   
   ' A member that is not serialized.
   <NonSerialized()> Public member5 As String  
  
   
   Public Sub New()     
      member1 = 11
      member2 = "hello"
      member3 = "hello"
      member4 = 3.14159265
      member5 = "hello world!"
   End Sub
      
   
   Public Sub Print()      
      Console.WriteLine("member1 = '{0}'", member1)
      Console.WriteLine("member2 = '{0}'", member2)
      Console.WriteLine("member3 = '{0}'", member3)
      Console.WriteLine("member4 = '{0}'", member4)
      Console.WriteLine("member5 = '{0}'", member5)
   End Sub

End Class

備註

SerializableAttribute 屬性套用至類型,以指出可以使用二進位或 XML 序列化來序列化此類型的實例。 如果序列化物件圖形中的任何類型未 SerializableAttribute 套用 屬性,Common Language Runtime 就會擲 SerializationException 回 。

SerializableAttribute即使 類別也實作 ISerializable 介面來控制二進位序列化程式,仍套用 屬性。

當您將 SerializableAttribute 屬性套用至類型時,預設會序列化所有私用和公用欄位。 您可以實 ISerializable 作 介面來覆寫序列化程式,以更細微地控制二進位序列化。

或者,您可以將 屬性套用至 欄位, NonSerializedAttribute 以從序列化中排除欄位。 如果二進位可序列化類型的欄位包含指標、控制碼或特定環境特定的某些其他資料結構,而且無法在不同的環境中有意義地重新建構,則您可能想要將該 NonSerializedAttribute 屬性套用至該欄位。

如需使用屬性的詳細資訊,請參閱 屬性。 如需二進位序列化的詳細資訊,請參閱 System.Runtime.Serialization

注意

這個屬性不適用於使用 的 System.Text.Json JSON 序列化。

建構函式

SerializableAttribute()

初始化 SerializableAttribute 類別的新執行個體。

屬性

TypeId

在衍生類別中實作時,取得這個 Attribute 的唯一識別碼。

(繼承來源 Attribute)

方法

Equals(Object)

傳回值,這個值指出此執行個體是否與指定的物件相等。

(繼承來源 Attribute)
GetHashCode()

傳回這個執行個體的雜湊碼。

(繼承來源 Attribute)
GetType()

取得目前執行個體的 Type

(繼承來源 Object)
IsDefaultAttribute()

在衍生類別中覆寫時,表示這個執行個體的值是衍生類別的預設值。

(繼承來源 Attribute)
Match(Object)

在衍生類別中覆寫時,會傳回值,表示這個執行個體是否等於指定物件。

(繼承來源 Attribute)
MemberwiseClone()

建立目前 Object 的淺層複製。

(繼承來源 Object)
ToString()

傳回代表目前物件的字串。

(繼承來源 Object)

明確介面實作

_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr)

將一組名稱對應至一組對應的分派識別項 (Dispatch Identifier)。

(繼承來源 Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

擷取物件的類型資訊,可以用來取得介面的類型資訊。

(繼承來源 Attribute)
_Attribute.GetTypeInfoCount(UInt32)

擷取物件提供的類型資訊介面數目 (0 或 1)。

(繼承來源 Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

提供物件所公開的屬性和方法的存取權。

(繼承來源 Attribute)

適用於

另請參閱