本文整理汇总了C#中ValidationResults.Add方法的典型用法代码示例。如果您正苦于以下问题:C# ValidationResults.Add方法的具体用法?C# ValidationResults.Add怎么用?C# ValidationResults.Add使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ValidationResults
的用法示例。
在下文中一共展示了ValidationResults.Add方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: ValidateEntityAsync
/// <inheritdoc/>
public Task ValidateEntityAsync(
SubmitContext context,
ChangeSetEntry entry,
ValidationResults validationResults,
CancellationToken cancellationToken)
{
Ensure.NotNull(validationResults, "validationResults");
DataModificationEntry dataModificationEntry = entry as DataModificationEntry;
if (dataModificationEntry != null)
{
object entity = dataModificationEntry.Entity;
// TODO GitHubIssue#50 : should this PropertyDescriptorCollection be cached?
PropertyDescriptorCollection properties =
new DataAnnotations.AssociatedMetadataTypeTypeDescriptionProvider(entity.GetType())
.GetTypeDescriptor(entity).GetProperties();
DataAnnotations.ValidationContext validationContext = new DataAnnotations.ValidationContext(entity);
foreach (PropertyDescriptor property in properties)
{
validationContext.MemberName = property.Name;
IEnumerable<DataAnnotations.ValidationAttribute> validationAttributes =
property.Attributes.OfType<DataAnnotations.ValidationAttribute>();
foreach (DataAnnotations.ValidationAttribute validationAttribute in validationAttributes)
{
object value = property.GetValue(entity);
DataAnnotations.ValidationResult validationResult =
validationAttribute.GetValidationResult(value, validationContext);
if (validationResult != DataAnnotations.ValidationResult.Success)
{
validationResults.Add(new ValidationResult()
{
Id = validationAttribute.GetType().FullName,
Message = validationResult.ErrorMessage,
Severity = ValidationSeverity.Error,
Target = entity,
PropertyName = property.Name
});
}
}
}
}
return Task.WhenAll();
}
示例2: ValidateEntityAsync
/// <inheritdoc/>
public Task ValidateEntityAsync(
SubmitContext context,
ChangeSetEntry entry,
ValidationResults validationResults,
CancellationToken cancellationToken)
{
Ensure.NotNull(validationResults, "validationResults");
DataModificationEntry dataModificationEntry = entry as DataModificationEntry;
if (dataModificationEntry != null)
{
object entity = dataModificationEntry.Entity;
// TODO (.NETCORE): Use PropertyDescriptorCollection in .NET Core (when available?)
// TODO GitHubIssue#50 : should this PropertyDescriptorCollection be cached?
IEnumerable<PropertyInfo> properties = entity.GetType().GetTypeInfo().DeclaredProperties;
DataAnnotations.ValidationContext validationContext = new DataAnnotations.ValidationContext(entity);
foreach (var property in properties)
{
validationContext.MemberName = property.Name;
IEnumerable<DataAnnotations.ValidationAttribute> validationAttributes =
property.GetCustomAttributes().OfType<DataAnnotations.ValidationAttribute>();
foreach (var validationAttribute in validationAttributes)
{
object value = property.GetValue(entity);
DataAnnotations.ValidationResult validationResult =
validationAttribute.GetValidationResult(value, validationContext);
if (validationResult != DataAnnotations.ValidationResult.Success)
{
validationResults.Add(new ValidationResult()
{
Id = validationAttribute.GetType().FullName,
Message = validationResult.ErrorMessage,
Severity = ValidationSeverity.Error,
Target = entity,
PropertyName = property.Name
});
}
}
}
}
return Task.WhenAll();
}