Skip to content

Add generic methods to SerializationInfo class #24908

@weitzhandler

Description

@weitzhandler

Hi,

Proposal

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

Added API:

public class SerializationInfo {
  + public void AddValue<T>(string name, T value);
  + public T GetValue<T>(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.

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:

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)))
{
}

Metadata

Metadata

Assignees

Labels

api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-Serialization

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions