Skip to content

Fix InvalidCastException when deserializing some dictionary types#42835

Merged
layomia merged 1 commit into
dotnet:masterfrom
layomia:dictionary
Sep 30, 2020
Merged

Fix InvalidCastException when deserializing some dictionary types#42835
layomia merged 1 commit into
dotnet:masterfrom
layomia:dictionary

Conversation

@layomia

@layomia layomia commented Sep 29, 2020

Copy link
Copy Markdown
Contributor

Prevents InvalidCastException when deserializing the following dictionary types (exhaustive list) and the key type is not typeof(string):

  • typeof(IDictionary<,>)
  • typeof(ImmutableDictionary<,>)
  • typeof(IImmutableDictionary<,>)
  • typeof(IReadOnlyDictionary<,>)

The workaround for .NET 5 is to use a custom converter for these types, or use a different dictionary type.

Deserializing other dictionary types e.g. class MyCustomDictionary : IDictionary<int, int>, Dictionary<DateTime, string>, class MyCustomReadOnlyDictionary : IReadOnlyDictionary<Guid, int> etc works fine.

@layomia layomia added this to the 6.0.0 milestone Sep 29, 2020
@layomia layomia self-assigned this Sep 29, 2020

@jozkee jozkee left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks.

@layomia layomia merged commit f63b0c1 into dotnet:master Sep 30, 2020
@layomia layomia deleted the dictionary branch September 30, 2020 03:43
@danmoseley

Copy link
Copy Markdown
Member

Did this work in 3.1?

@jozkee

jozkee commented Sep 30, 2020

Copy link
Copy Markdown
Member

@danmosemsft support for non-string dictionary keys was added in 5.0 (this was not a regression from 3.1), if someone wants the feature to work on 3.1 and they specifically need one of the types fixed in this PR, they would have to use a daily build of .NET 6 or wait for .NET 6 preview 1.

@danmoseley

Copy link
Copy Markdown
Member

@jozkee sounds good

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants