Compiler Error CS0182 while setting ErrorMessage on MinLength Attribute #197
-
In order to provide localized ErrorMessage to the user, I'm trying to use a localized string taken from a resx file. public partial class MainWindowViewModel : ObservableValidator
{
[ObservableProperty] [Required] [MinLength(2, ErrorMessage = Resources.givenName)] [MaxLength(40)]
private string _firstName = string.Empty;
[ObservableProperty] [Required] [MinLength(2)] [MaxLength(40)]
private string _lastName = string.Empty;
} I guess I'm doing something wrong...is there a way to get the result I'm looking for? |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 1 reply
-
This is unrelated from the MVVM Toolkit, the snippet above is just invalid C# syntax. As the docs for the error also state (here), as well as the ECMA spec for the C# language, only compile time constants are allowed as input for attributes. In your case, that |
Beta Was this translation helpful? Give feedback.
This is unrelated from the MVVM Toolkit, the snippet above is just invalid C# syntax. As the docs for the error also state (here), as well as the ECMA spec for the C# language, only compile time constants are allowed as input for attributes. In your case, that
Resources.givenName
is not a constant, hence the error. You'll have to either make that a constant or come up with some other approach to wire this up (eg. a custom validation attribute taking the property name and then loading with reflection at runtime, or something) 🙂