Is it possible to specify field name using some strongly-typed way (lambda?) in such situation:
public class Demo : IValidatableObject
{
public string DemoField {get; set;}
IEnumerable<ValidationResult> IValidatableObject.Validate(ValidationContext validationContext)
{
if (<...>)
{
yield return new ValidationResult("Some validation message", new string[] { "DemoField" }); // <-- Here
}
}
}
When field name is specified in string it can’t be refactored, for example.
I am not aware of any “built-in” way. But you could use LINQ expressions to do something like this:
This way you have no hard-coded strings, and refactoring will work.