다음을 통해 공유


Binary serialization

Serialization is the process of storing the state of an object to a storage medium. In binary serialization, the public and private fields of the object and the name of the class, including the assembly containing the class, are converted to a stream of bytes, which is then written to a data stream. When the object is subsequently deserialized, an exact clone of the original object is created.

When implementing a serialization mechanism in an object-oriented environment, you often need to make tradeoffs between ease of use and flexibility. The process can be automated to a large extent, provided you are given sufficient control over the process. For example, situations may arise where simple binary serialization is not sufficient, or there might be a specific reason to decide which fields in a class need to be serialized. The following sections examine the robust serialization mechanism provided with .NET and highlight a number of important features that allow you to customize the process to meet your needs.

Note

The state of a UTF-8 or UTF-7 encoded object is not preserved if the object is serialized and deserialized using different .NET versions.

Warning

Binary serialization can be dangerous. For more information, see BinaryFormatter security guide.

Binary serialization allows modifying private members inside an object and therefore changing the state of it. Because of this, other serialization frameworks, like System.Text.Json, that operate on the public API surface are recommended.

.NET Core

.NET Core supports binary serialization for a subset of types. You can see the list of supported types in the Serializable types section that follows. The listed types are guaranteed to be serializable between .NET Framework 4.5.1 and later versions and between .NET Core 2.0 and later versions. Other .NET implementations, such as Mono, aren't officially supported but should also work.

Serializable types

Type Notes
Microsoft.CSharp.RuntimeBinder.RuntimeBinderException Starting in .NET Core 2.0.4.
Microsoft.CSharp.RuntimeBinder.RuntimeBinderInternalCompilerException Starting in .NET Core 2.0.4.
System.AccessViolationException Starting in .NET Core 2.0.4.
System.AggregateException Starting in .NET Core 2.0.4.
System.AppDomainUnloadedException Starting in .NET Core 2.0.4.
System.ApplicationException Starting in .NET Core 2.0.4.
System.ArgumentException Starting in .NET Core 2.0.4.
System.ArgumentNullException Starting in .NET Core 2.0.4.
System.ArgumentOutOfRangeException Starting in .NET Core 2.0.4.
System.ArithmeticException Starting in .NET Core 2.0.4.
System.Array
System.ArraySegment<T>
System.ArrayTypeMismatchException Starting in .NET Core 2.0.4.
System.Attribute
System.BadImageFormatException Starting in .NET Core 2.0.4.
System.Boolean
System.Byte
System.CannotUnloadAppDomainException Starting in .NET Core 2.0.4.
System.Char
System.Collections.ArrayList
System.Collections.BitArray
System.Collections.Comparer
System.Collections.DictionaryEntry
System.Collections.Generic.Comparer<T>
System.Collections.Generic.Dictionary<TKey,TValue>
System.Collections.Generic.EqualityComparer<T>
System.Collections.Generic.HashSet<T>
System.Collections.Generic.KeyNotFoundException Starting in .NET Core 2.0.4.
System.Collections.Generic.KeyValuePair<TKey,TValue>
System.Collections.Generic.LinkedList<T>
System.Collections.Generic.List<T>
System.Collections.Generic.Queue<T>
System.Collections.Generic.SortedDictionary<TKey,TValue>
System.Collections.Generic.SortedList<TKey,TValue>
System.Collections.Generic.SortedSet<T>
System.Collections.Generic.Stack<T>
System.Collections.Hashtable
System.Collections.ObjectModel.Collection<T>
System.Collections.ObjectModel.KeyedCollection<TKey,TItem>
System.Collections.ObjectModel.ObservableCollection<T>
System.Collections.ObjectModel.ReadOnlyCollection<T>
System.Collections.ObjectModel.ReadOnlyDictionary<TKey,TValue>
System.Collections.ObjectModel.ReadOnlyObservableCollection<T>
System.Collections.Queue
System.Collections.SortedList
System.Collections.Specialized.HybridDictionary
System.Collections.Specialized.ListDictionary
System.Collections.Specialized.OrderedDictionary
System.Collections.Specialized.StringCollection
System.Collections.Specialized.StringDictionary
System.Collections.Stack
System.Collections.Generic.NonRandomizedStringEqualityComparer Starting in .NET Core 2.0.4.
System.ComponentModel.BindingList<T>
System.ComponentModel.DataAnnotations.ValidationException Starting in .NET Core 2.0.4.
System.ComponentModel.Design.CheckoutException Starting in .NET Core 2.0.4.
System.ComponentModel.InvalidAsynchronousStateException Starting in .NET Core 2.0.4.
System.ComponentModel.InvalidEnumArgumentException Starting in .NET Core 2.0.4.
System.ComponentModel.LicenseException Starting in .NET Core 2.0.4.
Serialization from .NET Framework to .NET Core is not supported.
System.ComponentModel.WarningException Starting in .NET Core 2.0.4.
System.ComponentModel.Win32Exception Starting in .NET Core 2.0.4.
System.Configuration.ConfigurationErrorsException Starting in .NET Core 2.0.4.
System.Configuration.ConfigurationException Starting in .NET Core 2.0.4.
System.Configuration.Provider.ProviderException Starting in .NET Core 2.0.4.
System.Configuration.SettingsPropertyIsReadOnlyException Starting in .NET Core 2.0.4.
System.Configuration.SettingsPropertyNotFoundException Starting in .NET Core 2.0.4.
System.Configuration.SettingsPropertyWrongTypeException Starting in .NET Core 2.0.4.
System.ContextMarshalException Starting in .NET Core 2.0.4.
System.DBNull Starting in .NET Core 2.0.2 and later versions.
System.Data.Common.DbException Starting in .NET Core 2.0.4.
System.Data.ConstraintException Starting in .NET Core 2.0.4.
System.Data.DBConcurrencyException Starting in .NET Core 2.0.4.
System.Data.DataException Starting in .NET Core 2.0.4.
System.Data.DataSet
System.Data.DataTable If you set RemotingFormat to SerializationFormat.Binary, it can only be exchanged with .NET Core 2.1 and later versions.
System.Data.DeletedRowInaccessibleException Starting in .NET Core 2.0.4.
System.Data.DuplicateNameException Starting in .NET Core 2.0.4.
System.Data.EvaluateException Starting in .NET Core 2.0.4.
System.Data.InRowChangingEventException Starting in .NET Core 2.0.4.
System.Data.InvalidConstraintException Starting in .NET Core 2.0.4.
System.Data.InvalidExpressionException Starting in .NET Core 2.0.4.
System.Data.MissingPrimaryKeyException Starting in .NET Core 2.0.4.
System.Data.NoNullAllowedException Starting in .NET Core 2.0.4.
System.Data.Odbc.OdbcException Starting in .NET Core 2.0.4.
System.Data.OperationAbortedException Starting in .NET Core 2.0.4.
System.Data.PropertyCollection
System.Data.ReadOnlyException Starting in .NET Core 2.0.4.
System.Data.RowNotInTableException Starting in .NET Core 2.0.4.
System.Data.SqlClient.SqlException Starting in .NET Core 2.0.4.
Serialization from .NET Framework to .NET Core is not supported
System.Data.SqlTypes.SqlAlreadyFilledException Starting in .NET Core 2.0.4.
System.Data.SqlTypes.SqlBoolean
System.Data.SqlTypes.SqlByte
System.Data.SqlTypes.SqlDateTime
System.Data.SqlTypes.SqlDouble
System.Data.SqlTypes.SqlGuid
System.Data.SqlTypes.SqlInt16
System.Data.SqlTypes.SqlInt32
System.Data.SqlTypes.SqlInt64
System.Data.SqlTypes.SqlNotFilledException Starting in .NET Core 2.0.4.
System.Data.SqlTypes.SqlNullValueException Starting in .NET Core 2.0.4.
System.Data.SqlTypes.SqlString
System.Data.SqlTypes.SqlTruncateException Starting in .NET Core 2.0.4.
System.Data.SqlTypes.SqlTypeException Starting in .NET Core 2.0.4.
System.Data.StrongTypingException Starting in .NET Core 2.0.4.
System.Data.SyntaxErrorException Starting in .NET Core 2.0.4.
System.Data.VersionNotFoundException Starting in .NET Core 2.0.4.
System.DataMisalignedException Starting in .NET Core 2.0.4.
System.DateTime
System.DateTimeOffset
System.Decimal
System.Diagnostics.Contracts.ContractException Starting in .NET Core 2.0.4.
System.Diagnostics.Tracing.EventSourceException Starting in .NET Core 2.0.4.
System.IO.DirectoryNotFoundException Starting in .NET Core 2.0.4.
System.DirectoryServices.AccountManagement.MultipleMatchesException Starting in .NET Core 2.0.4.
System.DirectoryServices.AccountManagement.NoMatchingPrincipalException Starting in .NET Core 2.0.4.
System.DirectoryServices.AccountManagement.PasswordException Starting in .NET Core 2.0.4.
System.DirectoryServices.AccountManagement.PrincipalException Starting in .NET Core 2.0.4.
System.DirectoryServices.AccountManagement.PrincipalExistsException Starting in .NET Core 2.0.4.
System.DirectoryServices.AccountManagement.PrincipalOperationException Starting in .NET Core 2.0.4.
System.DirectoryServices.AccountManagement.PrincipalServerDownException Starting in .NET Core 2.0.4.
System.DirectoryServices.ActiveDirectory.ActiveDirectoryObjectExistsException Starting in .NET Core 2.0.4.
System.DirectoryServices.ActiveDirectory.ActiveDirectoryObjectNotFoundException Starting in .NET Core 2.0.4.
System.DirectoryServices.ActiveDirectory.ActiveDirectoryOperationException Starting in .NET Core 2.0.4.
System.DirectoryServices.ActiveDirectory.ActiveDirectoryServerDownException Starting in .NET Core 2.0.4.
System.DirectoryServices.ActiveDirectory.ForestTrustCollisionException Starting in .NET Core 2.0.4.
System.DirectoryServices.ActiveDirectory.SyncFromAllServersOperationException Starting in .NET Core 2.0.4.
System.DirectoryServices.DirectoryServicesCOMException Starting in .NET Core 2.0.4.
System.DirectoryServices.Protocols.BerConversionException Starting in .NET Core 2.0.4.
System.DirectoryServices.Protocols.DirectoryException Starting in .NET Core 2.0.4.
System.DirectoryServices.Protocols.DirectoryOperationException Starting in .NET Core 2.0.4.
System.DirectoryServices.Protocols.LdapException Starting in .NET Core 2.0.4.
System.DirectoryServices.Protocols.TlsOperationException Starting in .NET Core 2.0.4.
System.DivideByZeroException Starting in .NET Core 2.0.4.
System.DllNotFoundException Starting in .NET Core 2.0.4.
System.Double
System.Drawing.Bitmap Starting in .NET Core 3.0
System.Drawing.Color
System.Drawing.Icon Starting in .NET Core 3.0
System.Drawing.Image Starting in .NET Core 3.0
System.Drawing.Imaging.Metafile Starting in .NET Core 3.0
System.Drawing.Point
System.Drawing.PointF
System.Drawing.Rectangle
System.Drawing.RectangleF
System.Drawing.Size
System.Drawing.SizeF
System.DuplicateWaitObjectException Starting in .NET Core 2.0.4.
System.EntryPointNotFoundException Starting in .NET Core 2.0.4.
System.Enum
System.EventArgs Starting in .NET Core 2.0.6.
System.Exception
System.ExecutionEngineException Starting in .NET Core 2.0.4.
System.FieldAccessException Starting in .NET Core 2.0.4.
System.FormatException Starting in .NET Core 2.0.4.
System.Globalization.CompareInfo
System.Globalization.CultureNotFoundException Starting in .NET Core 2.0.4.
System.Globalization.SortVersion
System.Guid
System.IO.Compression.ZLibException Starting in .NET Core 2.0.4.
System.IO.DriveNotFoundException Starting in .NET Core 2.0.4.
System.IO.EndOfStreamException Starting in .NET Core 2.0.4.
System.IO.FileFormatException Starting in .NET Core 2.0.4.
System.IO.FileLoadException Starting in .NET Core 2.0.4.
System.IO.FileNotFoundException Starting in .NET Core 2.0.4.
System.IO.IOException Starting in .NET Core 2.0.4.
System.IO.InternalBufferOverflowException Starting in .NET Core 2.0.4.
System.IO.InvalidDataException Starting in .NET Core 2.0.4.
System.IO.IsolatedStorage.IsolatedStorageException Starting in .NET Core 2.0.4.
System.IO.PathTooLongException Starting in .NET Core 2.0.4.
System.IndexOutOfRangeException Starting in .NET Core 2.0.4.
System.InsufficientExecutionStackException Starting in .NET Core 2.0.4.
System.InsufficientMemoryException Starting in .NET Core 2.0.4.
System.Int16
System.Int32
System.Int64
System.IntPtr
System.InvalidCastException Starting in .NET Core 2.0.4.
System.InvalidOperationException Starting in .NET Core 2.0.4.
System.InvalidProgramException Starting in .NET Core 2.0.4.
System.InvalidTimeZoneException Starting in .NET Core 2.0.4.
System.MemberAccessException Starting in .NET Core 2.0.4.
System.MethodAccessException Starting in .NET Core 2.0.4.
System.MissingFieldException Starting in .NET Core 2.0.4.
System.MissingMemberException Starting in .NET Core 2.0.4.
System.MissingMethodException Starting in .NET Core 2.0.4.
System.MulticastNotSupportedException Starting in .NET Core 2.0.4.
System.Net.Cookie
System.Net.CookieCollection
System.Net.CookieContainer
System.Net.CookieException Starting in .NET Core 2.0.4.
System.Net.HttpListenerException Starting in .NET Core 2.0.4.
System.Net.Mail.SmtpException Starting in .NET Core 2.0.4.
System.Net.Mail.SmtpFailedRecipientException Starting in .NET Core 2.0.4.
System.Net.Mail.SmtpFailedRecipientsException Starting in .NET Core 2.0.4.
System.Net.NetworkInformation.NetworkInformationException Starting in .NET Core 2.0.4.
System.Net.NetworkInformation.PingException Starting in .NET Core 2.0.4.
System.Net.ProtocolViolationException Starting in .NET Core 2.0.4.
System.Net.Sockets.SocketException Starting in .NET Core 2.0.4.
System.Net.WebException Starting in .NET Core 2.0.4.
System.Net.WebSockets.WebSocketException Starting in .NET Core 2.0.4.
System.NotFiniteNumberException Starting in .NET Core 2.0.4.
System.NotImplementedException Starting in .NET Core 2.0.4.
System.NotSupportedException Starting in .NET Core 2.0.4.
System.NullReferenceException Starting in .NET Core 2.0.4.
System.Nullable<T>
System.Numerics.BigInteger
System.Numerics.Complex
System.Object
System.ObjectDisposedException Starting in .NET Core 2.0.4.
System.OperationCanceledException Starting in .NET Core 2.0.4.
System.OutOfMemoryException Starting in .NET Core 2.0.4.
System.OverflowException Starting in .NET Core 2.0.4.
System.PlatformNotSupportedException Starting in .NET Core 2.0.4.
System.RankException Starting in .NET Core 2.0.4.
System.Reflection.AmbiguousMatchException Starting in .NET Core 2.0.4.
System.Reflection.CustomAttributeFormatException Starting in .NET Core 2.0.4.
System.Reflection.InvalidFilterCriteriaException Starting in .NET Core 2.0.4.
System.Reflection.ReflectionTypeLoadException Starting in .NET Core 2.0.4.
Serialization from .NET Framework to .NET Core is not supported.
System.Reflection.TargetException Starting in .NET Core 2.0.4.
System.Reflection.TargetInvocationException Starting in .NET Core 2.0.4.
System.Reflection.TargetParameterCountException Starting in .NET Core 2.0.4.
System.Resources.MissingManifestResourceException Starting in .NET Core 2.0.4.
System.Resources.MissingSatelliteAssemblyException Starting in .NET Core 2.0.4.
System.Runtime.CompilerServices.RuntimeWrappedException Starting in .NET Core 2.0.4.
System.Runtime.InteropServices.COMException Starting in .NET Core 2.0.4.
System.Runtime.InteropServices.ExternalException Starting in .NET Core 2.0.4.
System.Runtime.InteropServices.InvalidComObjectException Starting in .NET Core 2.0.4.
System.Runtime.InteropServices.InvalidOleVariantTypeException Starting in .NET Core 2.0.4.
System.Runtime.InteropServices.MarshalDirectiveException Starting in .NET Core 2.0.4.
System.Runtime.InteropServices.SEHException Starting in .NET Core 2.0.4.
System.Runtime.InteropServices.SafeArrayRankMismatchException Starting in .NET Core 2.0.4.
System.Runtime.InteropServices.SafeArrayTypeMismatchException Starting in .NET Core 2.0.4.
System.Runtime.Serialization.InvalidDataContractException Starting in .NET Core 2.0.4.
System.Runtime.Serialization.SerializationException Starting in .NET Core 2.0.4.
System.SByte
System.Security.AccessControl.PrivilegeNotHeldException Starting in .NET Core 2.0.4.
System.Security.Authentication.AuthenticationException Starting in .NET Core 2.0.4.
System.Security.Authentication.InvalidCredentialException Starting in .NET Core 2.0.4.
System.Security.Cryptography.CryptographicException Starting in .NET Core 2.0.4.
System.Security.Cryptography.CryptographicUnexpectedOperationException Starting in .NET Core 2.0.4.
System.Security.Cryptography.Xml.CryptoSignedXmlRecursionException Starting in .NET Core 2.0.4.
System.Security.HostProtectionException Starting in .NET Core 2.0.4.
System.Security.Policy.PolicyException Starting in .NET Core 2.0.4.
System.Security.Principal.IdentityNotMappedException Starting in .NET Core 2.0.4.
System.Security.SecurityException Starting in .NET Core 2.0.4.
Limited serialization data.
System.Security.VerificationException Starting in .NET Core 2.0.4.
System.Security.XmlSyntaxException Starting in .NET Core 2.0.4.
System.ServiceProcess.TimeoutException Starting in .NET Core 2.0.4.
System.Single
System.StackOverflowException Starting in .NET Core 2.0.4.
System.String
System.StringComparer
System.SystemException Starting in .NET Core 2.0.4.
System.Text.DecoderFallbackException Starting in .NET Core 2.0.4.
System.Text.EncoderFallbackException Starting in .NET Core 2.0.4.
System.Text.RegularExpressions.RegexMatchTimeoutException Starting in .NET Core 2.0.4.
System.Text.StringBuilder
System.Threading.AbandonedMutexException Starting in .NET Core 2.0.4.
System.Threading.BarrierPostPhaseException Starting in .NET Core 2.0.4.
System.Threading.LockRecursionException Starting in .NET Core 2.0.4.
System.Threading.SemaphoreFullException Starting in .NET Core 2.0.4.
System.Threading.SynchronizationLockException Starting in .NET Core 2.0.4.
System.Threading.Tasks.TaskCanceledException Starting in .NET Core 2.0.4.
System.Threading.Tasks.TaskSchedulerException Starting in .NET Core 2.0.4.
System.Threading.ThreadAbortException Starting in .NET Core 2.0.4.
System.Threading.ThreadInterruptedException Starting in .NET Core 2.0.4.
System.Threading.ThreadStartException Starting in .NET Core 2.0.4.
System.Threading.ThreadStateException Starting in .NET Core 2.0.4.
System.Threading.WaitHandleCannotBeOpenedException Starting in .NET Core 2.0.4.
System.TimeSpan
System.TimeZoneInfo.AdjustmentRule
System.TimeZoneInfo
System.TimeZoneNotFoundException Starting in .NET Core 2.0.4.
System.TimeoutException Starting in .NET Core 2.0.4.
System.Transactions.TransactionAbortedException Starting in .NET Core 2.0.4.
System.Transactions.TransactionException Starting in .NET Core 2.0.4.
System.Transactions.TransactionInDoubtException Starting in .NET Core 2.0.4.
System.Transactions.TransactionManagerCommunicationException Starting in .NET Core 2.0.4.
System.Transactions.TransactionPromotionException Starting in .NET Core 2.0.4.
System.Tuple
System.TypeAccessException Starting in .NET Core 2.0.4.
System.TypeInitializationException Starting in .NET Core 2.0.4.
System.TypeLoadException Starting in .NET Core 2.0.4.
System.TypeUnloadedException Starting in .NET Core 2.0.4.
System.UInt16
System.UInt32
System.UInt64
System.UIntPtr
System.UnauthorizedAccessException Starting in .NET Core 2.0.4.
System.Uri
System.UriFormatException Starting in .NET Core 2.0.4.
System.ValueTuple Not serializable in .NET Framework 4.7 and earlier versions.
System.ValueType
System.Version
System.WeakReference<T>
System.WeakReference
System.Xml.Schema.XmlSchemaException Starting in .NET Core 2.0.4.
System.Xml.Schema.XmlSchemaInferenceException Starting in .NET Core 2.0.4.
System.Xml.Schema.XmlSchemaValidationException Starting in .NET Core 2.0.4.
System.Xml.XPath.XPathException Starting in .NET Core 2.0.4.
System.Xml.XmlException Starting in .NET Core 2.0.4.
System.Xml.Xsl.XsltCompileException Starting in .NET Core 2.0.4.
System.Xml.Xsl.XsltException Starting in .NET Core 2.0.4.

See also