You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Immutable record types are not treated as immutable #777
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':
publicsealedclassMyRecord{publicstringproperty1{get;init;}publicintproperty2{get;init;}publicobjectpropertyEtc{get;init}publicMyRecord(stringproperty1,intproperty2,objectpropertyEtc){this.property1=property1;this.property2=property2;this.propertyEtc=propertyEtc;}//some other code, like Equals()}
The problem is that ParseArguments<MyRecord>() 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.
As far as I understand, the wiki on immutable types support, the class generally has all that's needed to work as an immutable, and the problem is that it is not regarded as such.
From what I see in ReflectionExtensions.IsMutable() the culpit is this line:
Just ran into a related issue. I wasn't making a record class, but a regular class with all of its properties readonly, but with init. That's obviously readonly for all intents and purposes, but CommandLine throws the same System.MissingMethodException as in Mikhail's case.
Activity
mikhail-barg commentedon Sep 23, 2021
As far as I understand, the wiki on immutable types support, the class generally has all that's needed to work as an immutable, and the problem is that it is not regarded as such.
From what I see in
ReflectionExtensions.IsMutable()the culpit is this line:which prevents
init-only properties from being treated as readonly.See for example this post on how to detect for
initproperties.V0ldek commentedon Jan 21, 2022
Just ran into a related issue. I wasn't making a record class, but a regular class with all of its properties readonly, but with
init. That's obviously readonly for all intents and purposes, but CommandLine throws the sameSystem.MissingMethodExceptionas in Mikhail's case.Add test for issue commandlineparser#777.
Implement fix for issue commandlineparser#777.