Runtime: Add generic methods to SerializationInfo class

Created on 4 Feb 2018  路  9Comments  路  Source: dotnet/runtime

Hi,

Proposal

Please add a couple of generic methods to the SerializationInfo class.

Added API:

```c#
public class SerializationInfo {

  • public void AddValue(string name, T value);
  • public T GetValue(string name);
    }
## Motive

Currently, when we want to use `SerializationInfo` in a serialization context (binary in my scenario), I have to convert all the fields back and forth.

Here's an example of how ugly this can get.

```c# 
protected CategoryGroupDefinition(SerializationInfo info, StreamingContext context)
  : this(
      (Guid)info.GetValue(nameof(Id), typeof(Guid)),
      info.GetString(nameof(Title)),
      (CategoryGroupDefinition)info.GetValue(nameof(Parent), typeof(CategoryGroupDefinition)),
      (GroupType)info.GetValue(nameof(Type), typeof(GroupType)),
      info.GetBoolean(nameof(IsMandatory)),
      (CategoryDefinition[])info.GetValue(nameof(Children), typeof(CategoryDefinition[])))
{
}

Adding some generic methods will shorten the above to:

c# protected CategoryGroupDefinition(SerializationInfo info, StreamingContext context) : this( info.GetValue<Guid>(nameof(Id)), info.GetString(nameof(Title)), info.GetValue<CategoryGroupDefinition>(nameof(Parent)), info.GetValue<GroupType>(nameof(Type)), info.GetBoolean(nameof(IsMandatory)), info.GetValue<CategoryDefinition[]>(nameof(Children))) { }

api-suggestion area-Serialization

Most helpful comment

@weitzhandler to be honest I'm not a fan of adding new APIs to the System.Runtime.Serialization.Formatters namespace. We don't want to encourage our customers to use BinaryFormatter and usually recommend to shift to other frameworks like Json.NET. Adding these helper methods conveys the wrong message IMO. I also doubt that it would pass api review as our PM has a strong opinion about binary serialization.

All 9 comments

cc @ViktorHofer as FYI

Although we prefer folks not write new code using BinaryFormatter, this seems a small change that makes a big improvement.

@danmosemsft feel free to assign me.

First Viktor would have to sponsor through API review...

@karelz changing from area-Serialization to area-System.Runtime.

@weitzhandler to be honest I'm not a fan of adding new APIs to the System.Runtime.Serialization.Formatters namespace. We don't want to encourage our customers to use BinaryFormatter and usually recommend to shift to other frameworks like Json.NET. Adding these helper methods conveys the wrong message IMO. I also doubt that it would pass api review as our PM has a strong opinion about binary serialization.

@ViktorHofer
Thanks for your response!
Can you please get a bit more detailed about the dark side of binary serialization?

I cite myself from here https://docs.microsoft.com/en-us/dotnet/standard/serialization/binary-serialization

As the nature of binary serialization allows the modification of private members inside an object and therefore changing the state of it, other serialization frameworks like JSON.NET which operate on the public API surface are recommended.

Thanks.
In some scenarios binary serialization is still the right option (my current case - where I want to save state of an object graph in app as it is).
Binary serialization also makes it easier to serialize using constructor.

Anyway I understand why the API shouldn't be expanded.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

omajid picture omajid  路  3Comments

chunseoklee picture chunseoklee  路  3Comments

yahorsi picture yahorsi  路  3Comments

btecu picture btecu  路  3Comments

omariom picture omariom  路  3Comments