commandlineparser / commandlineparser/commandline

Immutable record types are not treated as immutable

Offen
#777 2 Kommentare 4 Reaktionen 0 zugewiesene Personen Auf GitHub ansehen
Vorherrschende Sprache
C#
Sterne
4.8k
Forks
478
PR-Merge-Kennzahlen
Keine gemergten PRs in 30 T.

Beschreibung

C# 9.0 syntax allows now to declare reference types as 'records' which is a nice syntax sugar for (immutable) data-container classes. There's a way to declare a record type as ['immutable'](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/record#immutability):
```c#
public sealed record MyRecord(
string property1,
int property2,
object propertyEtc
)
{}
```
This would actually generate a following class:
```c#
public sealed class MyRecord
{
public string property1 {get; init;}
public int property2 {get; init;}
public object propertyEtc {get; init}

public MyRecord(string property1, int property2, object propertyEtc)
{
this.property1 = property1;
this.property2 = property2;
this.propertyEtc = propertyEtc;
}
//some other code, like Equals()
}
```

The problem is that `ParseArguments()` would fail with `System.MissingMethodException: No parameterless constructor defined for type 'MyRecord '.` (which there's really none).

While I could declare a prarmeterless constructor for such record, it would be really great to have such classes supported as-is.

Beitragsleitfaden

Für dieses Repository ist kein Beitragsleitfaden indexiert

Rechercherichtung

Start by reproducing the failure with ParseArguments() using the immutable C# record shown in the issue. Trace how parsing constructs the target type and handles its init-only properties, then verify that the same record parses without a parameterless constructor and no longer raises MissingMethodException.

Vom Indexierungsmodell aus dem Issue-Text verfasst.

Bewertung

Tech-Stack
csharp
Bereich
cli
Issue-Typ
Bug
Schwierigkeit
4/5
Geschätzter Aufwand
3-5 Tage
Aktivitätsstatus
Veraltet
Klarheit
Größtenteils klar
Anfängerfreundlichkeit
35/100

Neue Issues direkt in Ihr Postfach

Eine kurze Übersicht über anfängerfreundliche GitHub-Issues.