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.

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] and ISerializable). 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 to ISerializable 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.

See also