SYSLIB0050: Formatter-based serialization is obsolete
The following APIs are obsolete, starting in .NET 8. Calling them in code generates warning SYSLIB0050
at compile time.
- System.Runtime.Serialization.FormatterConverter
- System.Runtime.Serialization.FormatterServices
- System.Runtime.Serialization.IFormatterConverter
- System.Runtime.Serialization.IObjectReference
- System.Runtime.Serialization.ISafeSerializationData
- System.Runtime.Serialization.ISerializationSurrogate
- System.Runtime.Serialization.ISurrogateSelector
- System.Runtime.Serialization.ObjectIDGenerator
- System.Runtime.Serialization.ObjectManager
- System.Runtime.Serialization.SafeSerializationEventArgs
- System.Runtime.Serialization.SerializationObjectManager
- System.Runtime.Serialization.StreamingContextStates
- System.Runtime.Serialization.SurrogateSelector
- System.Runtime.Serialization.Formatters.FormatterAssemblyStyle
- System.Runtime.Serialization.Formatters.FormatterTypeStyle
- System.Runtime.Serialization.Formatters.IFieldInfo
- System.Runtime.Serialization.Formatters.TypeFilterLevel
- System.Type.IsSerializable
- System.Reflection.FieldAttributes.NotSerialized
- System.Reflection.FieldInfo.IsNotSerialized
- System.Reflection.TypeAttributes.Serializable
- System.Runtime.Serialization.ISerializable.GetObjectData(SerializationInfo, StreamingContext)
- SerializationInfo(Type, IFormatterConverter, Boolean)
- SerializationInfo(Type, IFormatterConverter)
- StreamingContext(StreamingContextStates, Object)
- StreamingContext(StreamingContextStates)
Workaround
If you were using FormatterServices.GetUninitializedObject(Type), use RuntimeHelpers.GetUninitializedObject(Type) instead.
If you cross-compile for .NET Framework and modern .NET, you can use an
#if
statement to selectively call the appropriate API, as shown in the following snippet.Type typeToInstantiate; #if NET5_0_OR_GREATER object obj = System.Runtime.CompilerServices.RuntimeHelpers.GetUninitializedObject(typeToInstantiate); #else object obj = System.Runtime.Serialization.FormatterServices.GetUninitializedObject(typeToInstantiate); #endif
If you're writing a serialization library, we strongly recommend against serialization libraries that support the legacy serialization infrastructure (
[Serializable]
andISerializable
). Modern serialization libraries should have policy based on a type's public APIs rather than its private implementation details. If you base a serializer on these implementation details and strongly tie it toISerializable
and other mechanisms that encourage embedding type names within the serialized payload, it can lead to the problems described in Deserialization risks in use of BinaryFormatter and related types.If your serialization library must remain compatible with the legacy serialization infrastructure, you can easily suppress the legacy serialization API obsoletions.
Suppress a warning
If you must use the obsolete APIs, you can suppress the warning in code or in your project file.
To suppress only a single violation, add preprocessor directives to your source file to disable and then re-enable the warning.
// Disable the warning.
#pragma warning disable SYSLIB0050
// Code that uses obsolete API.
// ...
// Re-enable the warning.
#pragma warning restore SYSLIB0050
To suppress all the SYSLIB0050
warnings in your project, add a <NoWarn>
property to your project file.
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
...
<NoWarn>$(NoWarn);SYSLIB0050</NoWarn>
</PropertyGroup>
</Project>
For more information, see Suppress warnings.