13 Replies
The
AllowedValues
attribute seems to be for code clarity/readability, not actually enforcing the value.
But, it's a property, so you can just implement that check in the setter.It would be nice if they put something like that in the documentation though.
https://learn.microsoft.com/en-us/dotnet/api/system.componentmodel.dataannotations.allowedvaluesattribute?view=net-8.0
AllowedValuesAttribute Class (System.ComponentModel.DataAnnotations)
Specifies a list of values that should be allowed in a property.
It just says "specifies a list of values that should be allowed"
There's got to be at least an analyzer for this
It would pretty easy to detect -- just look at constructors where that property is assigned a constant. Roslyn detects that automatically.
More complex rules will have to be coded manually though
@Jason_Bjorn all of DataAnnotations is for things like GUIs and ASP.NET when doing input validation. They have no meaning to the compiler.
the docs are pretty bleak for that one
would be nice if this was documented
but thank you
It is.
Attributes interpreted by the compiler are documented as such.
Where does it say that
What do you mean?
If the docs for an attribute don’t say “the compiler looks at this attribute” (paraphrasing), the compiler won’t be looking at the attribute.
i think this one is mostly on microsft (or the team of people who update the docs)
it would be nice if there were a tag or something that clarified that this does nothing
another thing i noticed for this, even if the attribute actually did something, this still wouldnt work as intended because the constructor only takes an array of objects and notes nothing about the last being an error message
so it could be rewritten as
thank you
Unknown User•5mo ago
Message Not Public
Sign In & Join Server To View
If you have no further questions, please use /close to mark the forum thread as answered