System Namespace


.NET Framework Class Library
SerializableAttribute Class

Indicates that a class can be serialized. This class cannot be inherited.

Namespace:  System
Assembly:  mscorlib (in mscorlib.dll)
Syntax

Visual Basic (Declaration)
<ComVisibleAttribute(True)> _
<AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Struct Or AttributeTargets.Enum Or AttributeTargets.Delegate, Inherited := False)> _
Public NotInheritable Class SerializableAttribute _
    Inherits Attribute
Visual Basic (Usage)
Dim instance As SerializableAttribute
C#
[ComVisibleAttribute(true)]
[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Struct|AttributeTargets.Enum|AttributeTargets.Delegate, Inherited = false)]
public sealed class SerializableAttribute : Attribute
Visual C++
[ComVisibleAttribute(true)]
[AttributeUsageAttribute(AttributeTargets::Class|AttributeTargets::Struct|AttributeTargets::Enum|AttributeTargets::Delegate, Inherited = false)]
public ref class SerializableAttribute sealed : public Attribute
JScript
public final class SerializableAttribute extends Attribute
Remarks

Apply the SerializableAttribute attribute to a type to indicate that instances of this type can be serialized. The common language runtime throws SerializationException if any type in the graph of objects being serialized does not have the SerializableAttribute attribute applied.

Apply the SerializableAttribute attribute even if the class also implements the ISerializable interface to control the serialization process.

All the public and private fields in a type that are marked by the SerializableAttribute are serialized by default, unless the type implements the ISerializable interface to override the serialization process. The default serialization process excludes fields that are marked with the NonSerializedAttribute attribute. If a field of a serializable type contains a pointer, a handle, or some other data structure that is specific to a particular environment, and cannot be meaningfully reconstituted in a different environment, then you might want to apply the NonSerializedAttribute attribute to that field.

For more information about using attributes, see Extending Metadata Using Attributes. For more information about serialization, see System.Runtime.Serialization.

Windows Mobile for Pocket PC, Windows Mobile for Smartphone, Windows CE Platform Note: The SerializableAttribute attribute is present in the .NET Compact Framework for compatibility only, and will be ignored if used.

Examples

The following example demonstrates serialization of an object that is marked with the SerializableAttribute attribute. To use the BinaryFormatter instead of the SoapFormatter, uncomment the appropriate lines.

Visual Basic
Imports System
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 'Main

End Class 'Test


' 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 'New


   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 'Print

End Class 'TestSimpleObject
C#
using System;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Soap;
//using System.Runtime.Serialization.Formatters.Binary;

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();

      //BinaryFormatter formatter = new BinaryFormatter();

      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();

      //formatter = new BinaryFormatter();

      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);
    }
}
Visual C++
#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;

   //BinaryFormatter* formatter = new BinaryFormatter();
   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;

   //formatter = new BinaryFormatter();
   obj = dynamic_cast<TestSimpleObject^>(formatter->Deserialize( stream ));
   stream->Close();
   Console::WriteLine( "" );
   Console::WriteLine( "After deserialization the object contains: " );
   obj->Print();
}

CPP_OLD
#using <mscorlib.dll>
#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]        
__gc class TestSimpleObject 
{
    int member1;
    String* member2;
    String* member3;
    double member4;
public:
    // A field that is not serialized.
    [NonSerialized] String* member5; 

    TestSimpleObject() 
    {
        member1 = 11;
        member2 = S"hello";
        member3 = S"hello";
        member4 = 3.14159265;
        member5 = S"hello world!";
    }

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

int main()
{
    //Creates a new TestSimpleObject object.
    TestSimpleObject* obj = new TestSimpleObject();

    Console::WriteLine(S"Before serialization the Object* contains: ");
    obj->Print();

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

    //BinaryFormatter* formatter = new BinaryFormatter();

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

    //Empties obj.
    obj = 0;

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

    obj = dynamic_cast<TestSimpleObject*>(formatter->Deserialize(stream));
    stream->Close();

    Console::WriteLine(S"");
    Console::WriteLine(S"After deserialization the object contains: ");
    obj->Print();
}
Inheritance Hierarchy

System..::.Object
  System..::.Attribute
    System..::.SerializableAttribute
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0, 1.1, 1.0
See Also

Reference

Other Resources

Tags :


Community Content

David M. Kean - MSFT
Pseudo attribute that does not appear as a normal custom attributes
Make note that this is a pseudo attribute that does not appear as a normal attribute when querying via Type.GetCustomAttributes. Instead, use the Type.IsSerializable property to determine if this attribute has been applied to a type.
Tags :

Mythgarr
Ambiguous intellisense description - consider revision
The intellisense and description for this attribute can be misleading at first blush. The text "This class cannot be inherited" when referring to an attribute might falsely imply that the class the attribute is applied to cannot be inherited.
Tags :

Page view tracker