question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Support C# 9.0 records

See original GitHub issue

With the advent of .NET 5.0 and C# 9 we now have a new, immutable-on-demand, kind of class with additional special features called record. Would it be possible to add support for them (especially the type known as positional record) out-of-the-box?

Right now using them as if they were a normal class gives this exception:

image

public record SampleModel(string Name, int Age);

var sample = JsonSerializer.Generic.Utf8.Deserialize<SampleModel, ExcludeNullsCamelCaseResolver<byte>>(Encoding.UTF8.GetBytes(jsonString));

Positional records only have one public constructor:

var sample = new SampleModel("Name", 100);

Issue Analytics

  • State:closed
  • Created 3 years ago
  • Comments:8 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Tornhoofcommented, Nov 16, 2020

I published 3.1.0-rc1 to include the records stuff. It was a bit more complicated than expected, as records also support a mixed concept where not only those positional properties are used but additionally normal properties. SpanJson did not support that by now (as soon as you used the constructor attribute, it only used that and nothing else, other properties would be ignored). So I had to extend it for this functionality.

1reaction
Tornhoofcommented, Nov 15, 2020

Yeah, I plan to add support for it in the next few days. I just haven’t looked into records much yet.

Read more comments on GitHub >

github_iconTop Results From Across the Web

What's new in C# 9.0 - C# Guide
C# 9.0 introduces record types. You use the record keyword to define a reference type that provides built-in functionality for encapsulating ...
Read more >
Records in C# 9 - InfoQ
C# 9 introduces records, a new reference type for encapsulating data developers can use instead of classes and structs.
Read more >
C# 9.0 - Introduction To Record Types
As we know in F#, everything is considered as immutable, and similarly in C# record types help us to work with immutable types....
Read more >
How to work with record types in C# 9
A record type in C# 9 is a lightweight, immutable data type (or a lightweight class) that has read-only properties only. Because a...
Read more >
Support for C# 9.0 in Visual Studio
When I try to use the new record keyword (or the init ), I get an unexpected token error in Visual Studio. What...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found