ObjectDumper icon indicating copy to clipboard operation
ObjectDumper copied to clipboard

C# 9 / dotnet 5.0 records compatible C# dump style

Open vincentremond opened this issue 4 years ago • 3 comments

With the new type record like this : public record Sprint(int SprintId, DateTimeOffset StartDate, DateTimeOffset EndDate);

When using DumpStyle.CSharp , the generated code is incompatible :

new Sprint
{
	SprintId = 12,
	StartDate = DateTimeOffset.ParseExact("2021-02-18T00:00:00.0000000+01:00", "O", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind),
	EndDate = DateTimeOffset.ParseExact("2021-03-03T00:00:00.0000000+01:00", "O", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind)
}

It should be using the constructor instead of properties initialiser :

new Sprint(
	SprintId: 12,
	StartDate: DateTimeOffset.ParseExact("2021-02-18T00:00:00.0000000+01:00", "O", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind),
	EndDate: DateTimeOffset.ParseExact("2021-03-03T00:00:00.0000000+01:00", "O", CultureInfo.InvariantCulture, DateTimeStyles.RoundtripKind)
)

vincentremond avatar Jan 06 '21 10:01 vincentremond

We need to ensure compatibility with older c# versions. So, we could add the c# version to the DumpOptions and control the output accordingly. Are property initializers no longer supported in c# 9? Almost impossible?

thomasgalliker avatar Jan 06 '21 13:01 thomasgalliker

Yes sure, we need to keep it compatible with the older versions.

An option would be great, Property initializers will still be available.

A possible implementation would be to check if a class has only one constructor with all the parameters matching exactly all the properties.

vincentremond avatar Jan 07 '21 13:01 vincentremond

I will do one time donation for this option 20 USD

dzmitry-lahoda avatar May 10 '21 12:05 dzmitry-lahoda

This would be really nice as it's a pain in my current code-base as we have a lot of records and record structs that look like this:

  public record BuildPipeline(Guid ProjectId, string ProjectName, int BuildDefinitionId, string BuildDefinitionName, Guid RepositoryId, string RepositoryName, DateTime lastRun);
  public record ProjectInfo(Guid ProjectId, string ProjectName);
  public record BuildInfo(int BuildDefinitionId, string BuildDefinitionName);
  public record GitInfo(Guid RepositoryId, string RepositoryName, Uri path);
  public readonly record struct DevOpsPatCredentials(Uri Instance, string PAT);

And so your dumper no longer works as well, (using it a lot for making test-data), as it used to.

Any chance something will happen on this?

frankhaugen avatar Feb 06 '23 19:02 frankhaugen

Thanks for your vote. I see the need for record types dumping too, don‘t get me wrong. I‘m just running with low spare time to do it properly. Thanks for the test data above. I give it a try with these types. (Is your project on github too?)

thomasgalliker avatar Feb 07 '23 05:02 thomasgalliker

Does anyone of you know how we can programmatically distinguish between a regular class/struct and an instance of a record type? I find hints on Stackoverflow but not really working code. As far as I know, there is no obvious difference between instances of classes/structs and record types.

thomasgalliker avatar Feb 07 '23 06:02 thomasgalliker

Does anyone of you know how we can programmatically distinguish between a regular class/struct and an instance of a record type? I find hints on Stackoverflow but not really working code. As far as I know, there is no obvious difference between instances of classes/structs and record types.

If you use Roslyn the syntax node is og type RecordDeclarationSyntax.

frankhaugen avatar Feb 07 '23 07:02 frankhaugen

@frankhaugen I don‘t understand how RecordDeclarationSyntax could help solve this problem. Roslyn is the compiler platform we use. RecordDeclarationSyntax Is part of the CodeAnalysis namespace. I have no experience with it. Can you help?

thomasgalliker avatar Feb 08 '23 05:02 thomasgalliker

Guys, @frankhaugen, @vincentremond, there is a new pre-release nuget package available which includes the record types change. Can one of you let me know if this is more or less what you desired? https://www.nuget.org/packages/ObjectDumper.NET/4.0.3-pre

Thanks for your patience. It was quite an effort. Feedback is welcome.

thomasgalliker avatar Feb 24 '23 17:02 thomasgalliker

Hello ! I have not tested it yet, but based on the unit tests I am seing, it looks great !! Thank you !!

vincentremond avatar Feb 24 '23 22:02 vincentremond