为什么这总是回归真的?
class Program
{
static void Main(string[] args)
{
Person p = new Person();
p.Age = 24;
ICollection<ValidationResult> results = new Collection<ValidationResult>();
bool isValid = Validator.TryValidateObject(p, new ValidationContext(p, null, null), results);
Console.WriteLine("Valid = {0}",isValid);
foreach (var result in results)
{
Console.WriteLine(result.ErrorMessage);
}
Console.ReadKey();
}
}
public class Person
{
[Required(ErrorMessage = "You have to identify yourself!!")]
public int Id { get; set; }
public decimal Age { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
我的用法有什么问题?
我正在尝试找到验证MVVM中数据的最佳方法.目前,我正在尝试使用MVVM模式将IDataErrorInfo与Data Annotations一起使用.
然而,似乎没有任何工作,我不知道我可能做错了什么.我有类似的东西.
public class Person : IDataErrorInfo
{
[Required(ErrorMessage="Please enter your name")]
public string Name { get; set; }
public string Error
{
get { throw new NotImplementedException(); }
}
string IDataErrorInfo.this[string propertyName]
{
get
{
return OnValidate(propertyName);
}
}
protected virtual string OnValidate(string propertyName)
{
if (string.IsNullOrEmpty(propertyName))
throw new ArgumentException("Property may not be null or empty", propertyName);
string error = string.Empty;
var value = this.GetType().GetProperty(propertyName).GetValue(this, null);
var results = new List<ValidationResult>();
var context = new ValidationContext(this, null, …
Run Code Online (Sandbox Code Playgroud) 我需要从此类“tblAccounts”和“AccountNumber”的数据注释中返回别名。它使用 ServiceStack ORM Lite。
[Alias("tblAccounts")]
[Schema("task")]
public class Account : IHasId<int>
{
[Alias("AccountNumber")]
public int Id { get; set; }
[Required]
public int UnitId { get; set; }
[Required]
public int OldAccountNumber { get; set; }
[Required]
}
Run Code Online (Sandbox Code Playgroud) 我有一堂课说“请求”。它有两个属性,如下所示。收件人的电子邮件验证工作正常。但是,它不适用于收件人。
[EmailAddress]
public string Recipient { get; set; }
[EmailAddress]
public List<string> Recipients { get; set; }
Run Code Online (Sandbox Code Playgroud)
感谢任何帮助。