WPF開發中常常會用到ValidationAttribute, 這樣就能夠在程序中利用這個標籤,結合IDataErrorInfo反射來完成數據驗證。 c#
PropertyInfo prop = GetType().GetProperty(columnName); IEnumerable<ValidationAttribute> validationmap = prop.GetCustomAttributes(typeof(ValidationAttribute), true).Cast<ValidationAttribute>(); ValidationResult result;
調用ValidationAttribute的IsValid方法 this
result= v.GetValidationResult(prop.GetValue(this, null), ValidationContext); spa
if (result != ValidationResult.Success)
{
return result.ErrorMessage;
} code