This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
Note
Access to this page requires authorization. You can trysigning in orchanging directories.
Access to this page requires authorization. You can trychanging directories.
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Indicates that a class can be serialized using binary or XML serialization. This class cannot be inherited.
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 SerializableAttributeInherits Attribute
The following example demonstrates SOAP serialization of an object that's marked with theSerializableAttribute attribute.
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 Systemopen System.IOopen 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.IOImports System.Runtime.SerializationImports System.Runtime.Serialization.Formatters.SoapPublic 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 SubEnd 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 SubEnd Class
Apply theSerializableAttribute attribute to a type to indicate that instances of this type can be serialized using binary or XML serialization. The common language runtime throwsSerializationException if any type in the graph of objects being serialized does not have theSerializableAttribute attribute applied.
Apply theSerializableAttribute attribute even if the class also implements theISerializable interface to control the binary serialization process.
When you apply theSerializableAttribute attribute to a type, all private and public fields are serialized by default. You can control binary serialization more granularly by implementing theISerializable interface to override the serialization process.
Or you can exclude fields from serialization by applying theNonSerializedAttribute attribute to the field. If a field of a binary-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 theNonSerializedAttribute attribute to that field.
For more information about using attributes, seeAttributes. For more information about binary serialization, seeSystem.Runtime.Serialization.
Note
This attribute does not apply to JSON serialization usingSystem.Text.Json.
SerializableAttribute() | Initializes a new instance of theSerializableAttribute class. |
TypeId | When implemented in a derived class, gets a unique identifier for thisAttribute. (Inherited fromAttribute) |
Equals(Object) | Returns a value that indicates whether this instance is equal to a specified object. (Inherited fromAttribute) |
GetHashCode() | Returns the hash code for this instance. (Inherited fromAttribute) |
GetType() | Gets theType of the current instance. (Inherited fromObject) |
IsDefaultAttribute() | When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (Inherited fromAttribute) |
Match(Object) | When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited fromAttribute) |
MemberwiseClone() | Creates a shallow copy of the currentObject. (Inherited fromObject) |
ToString() | Returns a string that represents the current object. (Inherited fromObject) |
_Attribute.GetIDsOfNames(Guid, IntPtr, UInt32, UInt32, IntPtr) | Maps a set of names to a corresponding set of dispatch identifiers. (Inherited fromAttribute) |
_Attribute.GetTypeInfo(UInt32, UInt32, IntPtr) | Retrieves the type information for an object, which can be used to get the type information for an interface. (Inherited fromAttribute) |
_Attribute.GetTypeInfoCount(UInt32) | Retrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited fromAttribute) |
_Attribute.Invoke(UInt32, Guid, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) | Provides access to properties and methods exposed by an object. (Inherited fromAttribute) |
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?
Was this page helpful?
Need help with this topic?
Want to try using Ask Learn to clarify or guide you through this topic?