Skip to content

No direct replacement for ISerializable in .NET #123475

@alexey-leonovich

Description

@alexey-leonovich

Description

In .Net Framework we could have data classes in a netstandard20 library with a serializer-agnostic setup like that (simplified):

public interface IEditSession
{
    bool HasChanges { get; }
}

[Serializable]
public sealed class EditSession : IEditSession, ISerializable
{
    private readonly List<IEditActionBase> editActions = new List<IEditActionBase>();
    
    public bool HasChanges => editActions.Any();

    private EditSession (SerializationInfo info, StreamingContext context)
    {
        editActions = (List<IEditActionBase>)info.GetValue("editActions", typeof(List<IEditActionBase>));
    }

    public void GetObjectData(SerializationInfo info, StreamingContext context)
    {
        info.AddValue("editActions", editActions);
    }
}

Serializers respected that setup (no need to add any serializer-specific attributes/setup code).
In .NET ISerializable is obsoleted and modern serializers (like System.Text.Json) ignore it.
But no direct replacement is offered instead:

  1. [JsonInclude] or similar is serializer-specific attribute.
  2. JsonSerializerOptions.Converters is additional serializer-specific setup code.
  3. [OnSerializing], [OnDeserializing] and IDeserializationCallback cannot help because it lacks SerializationInfo (and they are also obsoleted starting from .NET 9.0).

Microsoft usually offers a direct replacement for obsoleted Api's (like AssemblyLoadContext instead of AppDomain.CreateDomain). But I see no netstandard20 option here (or maybe I've missed some idea?).
Consider adding a direct replacement for ISerializable in .NET, please.

Reproduction Steps

  1. Create a netstandard20 library with a data class for which you need to have same serialization/deserialization behavior with most serializers.
  2. That library shouldn't have serializer-specific references.
  3. There is no way to set a serializer-agnostic setup in .NET like it was in .Net Framework.

Expected behavior

A direct replacement for ISerializable is offered in .NET.

Actual behavior

There is no direct replacement for ISerializable in .NET.

Regression?

No response

Known Workarounds

No response

Configuration

No response

Other information

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-Serializationbinaryformatter-migrationIssues related to the removal of BinaryFormatter and migrations away from ituntriagedNew issue has not been triaged by the area owner

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions