microsoft / referencesource

Source from the Microsoft .NET Reference Source that represent a subset of the .NET Framework
https://referencesource.microsoft.com/
MIT License
3.17k stars 1.27k forks source link

CompareAttribute won't work on records values #191

Open Brannos1970 opened 1 year ago

Brannos1970 commented 1 year ago

The compare attribute when applied to a record parameter give the error message it is only valid on property or indexers.

Issue: I was using the compare in a record to validate passwords. The error message was display. When I created a custom comparer and added the flag attributes of Parameter and Fields it resolved the issue.

Proposed Solution:

Add the attribute usage flag "AttributeTargets.Parameter". The AttributeTargets.Fields may also need to be added.

svick commented 1 year ago

This repo is not the right place to report issues, suggest improvements or ask questions about .Net.

That being said, if you want to add an attribute to the property generated from a record primary constructor, you can do it by specifying property: on the attribute, e.g.:

record R([property: Compare("J")] int I, int J);

If you still think CompareAttribute should be changed, the right place to propose that is the dotnet/runtime repo.