-
Notifications
You must be signed in to change notification settings - Fork 5.3k
Description
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:
[JsonInclude]or similar is serializer-specific attribute.JsonSerializerOptions.Convertersis additional serializer-specific setup code.[OnSerializing],[OnDeserializing]andIDeserializationCallbackcannot help because it lacksSerializationInfo(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
- Create a
netstandard20library with a data class for which you need to have same serialization/deserialization behavior with most serializers. - That library shouldn't have serializer-specific references.
- 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