Ich verwende das folgende Muster beim Importieren von "unbekannten" Daten.AbstractValidator: zeigt Fehlermeldungen mit Eigenschaftswerten
public class MyCustomObject
{
public string MyCustomDateAsString { get; set; }
public DateTime? MyCustomDate
{
get
{
DateTime? returnValue = null;
DateTime parseResult = DateTime.MinValue;
bool parseAttempt = DateTime.TryParse(this.MyCustomDateAsString, out parseResult);
if (parseAttempt)
{
returnValue = parseResult;
}
return returnValue;
}
}
public string MyCustomIntAsString { get; set; }
public int? MyCustomInt
{
get
{
int? returnValue = null;
int parseResult = 0;
bool parseAttempt = int.TryParse(this.MyCustomIntAsString, out parseResult);
if (parseAttempt)
{
returnValue = parseResult;
}
return returnValue;
}
}
}
Ich habe das funktioniert.
public class MyCustomObjectValidator : AbstractValidator<MyCustomObject>
{
public MyCustomObjectValidator()
{
RuleFor(custobj => custobj.MyCustomDateAsString).NotEmpty().WithMessage("Please specify a MyCustomDateAsString");
RuleFor(custobj => custobj.MyCustomIntAsString).NotEmpty().WithMessage("Please specify a MyCustomIntAsString");
}
}
Ich möchte diese Regeln hinzuzufügen.
RuleFor(custobj => custobj.MyCustomDate).NotNull().WithMessage("MyCustomDate must be a valid non null date. You specified '{0}'"); /* How can I put MyCustomDateAsString for the {0} */
RuleFor(custobj => custobj.MyCustomInt).NotNull().WithMessage("MyCustomInt must be a valid non null int. You specified '{0}'"); /* How can I put MyCustomIntAsString for the {0} */
Aber ich weiß nicht, wie das MyCustomDateAsString und MyCustomIntAsString bekommen in den Fehlermeldungen für MyCustomDate und MyCustomInt zu zeigen.