SerializableAttribute Klasa

Definicja

Wskazuje, że klasę można serializować przy użyciu serializacji binarnej lub XML. Klasa ta nie może być dziedziczona.

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
Dziedziczenie
SerializableAttribute
Atrybuty

Przykłady

W poniższym przykładzie pokazano serializacji protokołu SOAP obiektu oznaczonego atrybutem SerializableAttribute .

#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

Uwagi

SerializableAttribute Zastosuj atrybut do typu, aby wskazać, że wystąpienia tego typu mogą być serializowane przy użyciu serializacji binarnej lub XML. Środowisko uruchomieniowe języka wspólnego zgłasza, SerializationException jeśli jakikolwiek typ na grafie obiektów, które są serializowane, nie ma zastosowanego atrybutu SerializableAttribute .

SerializableAttribute Zastosuj atrybut, nawet jeśli klasa implementuje ISerializable również interfejs do sterowania binarnym procesem serializacji.

Po zastosowaniu atrybutu SerializableAttribute do typu wszystkie pola prywatne i publiczne są domyślnie serializowane. Można kontrolować serializacji binarnej bardziej szczegółowo, implementując ISerializable interfejs w celu zastąpienia procesu serializacji.

Możesz też wykluczyć pola z serializacji, stosując NonSerializedAttribute atrybut do pola. Jeśli pole typu z możliwością serializacji binarnej zawiera wskaźnik, uchwyt lub inną strukturę danych specyficzną dla określonego środowiska i nie można znacząco odtworzyć w innym środowisku, możesz zastosować NonSerializedAttribute atrybut do tego pola.

Aby uzyskać więcej informacji na temat używania atrybutów, zobacz Atrybuty. Aby uzyskać więcej informacji na temat serializacji binarnej, zobacz System.Runtime.Serialization.

Uwaga

Ten atrybut nie ma zastosowania do serializacji JSON przy użyciu .System.Text.Json

Konstruktory

SerializableAttribute()

Inicjuje nowe wystąpienie klasy SerializableAttribute.

Właściwości

TypeId

Po zaimplementowaniu w klasie pochodnej pobiera unikatowy identyfikator dla tego Attributeelementu .

(Odziedziczone po Attribute)

Metody

Equals(Object)

Zwraca wartość wskazującą, czy to wystąpienie jest równe podanemu obiektowi.

(Odziedziczone po Attribute)
GetHashCode()

Zwraca wartość skrótu dla tego wystąpienia.

(Odziedziczone po Attribute)
GetType()

Type Pobiera wartość bieżącego wystąpienia.

(Odziedziczone po Object)
IsDefaultAttribute()

Podczas zastępowania w klasie pochodnej wskazuje, czy wartość tego wystąpienia jest wartością domyślną dla klasy pochodnej.

(Odziedziczone po Attribute)
Match(Object)

Po przesłonięciu w klasie pochodnej zwraca wartość wskazującą, czy to wystąpienie jest równe określonemu obiektowi.

(Odziedziczone po Attribute)
MemberwiseClone()

Tworzy płytkią kopię bieżącego Objectelementu .

(Odziedziczone po Object)
ToString()

Zwraca ciąg reprezentujący bieżący obiekt.

(Odziedziczone po Object)

Jawne implementacje interfejsu

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

Zestaw nazw jest mapowany na odpowiedni zestaw identyfikatorów wysyłania.

(Odziedziczone po Attribute)
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr)

Pobiera informacje o typie dla obiektu, który może służyć do pobierania informacji o typie dla interfejsu.

(Odziedziczone po Attribute)
_Attribute.GetTypeInfoCount(UInt32)

Pobiera informację o liczbie typów interfejsów, jakie zawiera obiekt (0 lub 1).

(Odziedziczone po Attribute)
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr)

Umożliwia dostęp do właściwości i metod udostępnianych przez obiekt.

(Odziedziczone po Attribute)

Dotyczy

Zobacz też