ObjectDumper
ObjectDumper copied to clipboard
C# 9 / dotnet 5.0 records compatible C# dump style
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)
)
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?
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.
I will do one time donation for this option 20 USD
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?
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?)
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.
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 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?
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.
Hello ! I have not tested it yet, but based on the unit tests I am seing, it looks great !! Thank you !!