Validator.TryValidateObjectAsync 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
重载
TryValidateObjectAsync(Object, ValidationContext, ICollection<ValidationResult>, CancellationToken)
- Source:
- Validator.cs
异步测试给定的对象实例是否有效。
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Constructing a ValidationContext without a display name is not trim-safe because it uses reflection to discover the type of the instance being validated in order to resolve the DisplayNameAttribute when a display name is not provided.")]
public static System.Threading.Tasks.Task<bool> TryValidateObjectAsync(object instance, System.ComponentModel.DataAnnotations.ValidationContext validationContext, System.Collections.Generic.ICollection<System.ComponentModel.DataAnnotations.ValidationResult>? validationResults, System.Threading.CancellationToken cancellationToken = default);
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Constructing a ValidationContext without a display name is not trim-safe because it uses reflection to discover the type of the instance being validated in order to resolve the DisplayNameAttribute when a display name is not provided.")>]
static member TryValidateObjectAsync : obj * System.ComponentModel.DataAnnotations.ValidationContext * System.Collections.Generic.ICollection<System.ComponentModel.DataAnnotations.ValidationResult> * System.Threading.CancellationToken -> System.Threading.Tasks.Task<bool>
Public Shared Function TryValidateObjectAsync (instance As Object, validationContext As ValidationContext, validationResults As ICollection(Of ValidationResult), Optional cancellationToken As CancellationToken = Nothing) As Task(Of Boolean)
参数
- instance
- Object
要测试的对象实例。 它不能 null。
- validationContext
- ValidationContext
描述要验证和提供验证器的服务和上下文的对象。
- validationResults
- ICollection<ValidationResult>
作为实例接收验证失败 ValidationResult 的可选集合。
- cancellationToken
- CancellationToken
等待任务完成时观察的 A CancellationToken 。
返回
Task<TResult>也就是说true,如果对象有效,false则遇到任何验证错误。
- 属性
例外
何时 instance 为 null。
何时 validationContext 为 null。
何时instance与 on validationContext不匹配ObjectInstance。
验证操作已取消。
注解
此方法计算附加到对象实例类型的所有 ValidationAttribute对象。 它还检查以确保已设置标记 RequiredAttribute 的所有属性。 除非调用validateAllProperties设置为 true,否则TryValidateObjectAsync(Object, ValidationContext, ICollection<ValidationResult>, Boolean, CancellationToken)它不会验证ValidationAttribute对象的单个属性上的 s。
验证分三个阶段运行:属性验证、类型级属性验证和对象级验证。 仅当前面的阶段成功时,每个阶段才会运行。 对象级验证更倾向于在实现时调用 ValidateAsync(ValidationContext, CancellationToken) ,而 Validate(ValidationContext) 对于仅实现同步接口的对象,则作为回退。 它不调用这两者。
适用于
TryValidateObjectAsync(Object, ValidationContext, ICollection<ValidationResult>, Boolean, CancellationToken)
- Source:
- Validator.cs
异步测试给定的对象实例是否有效。
[System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Constructing a ValidationContext without a display name is not trim-safe because it uses reflection to discover the type of the instance being validated in order to resolve the DisplayNameAttribute when a display name is not provided.")]
public static System.Threading.Tasks.Task<bool> TryValidateObjectAsync(object instance, System.ComponentModel.DataAnnotations.ValidationContext validationContext, System.Collections.Generic.ICollection<System.ComponentModel.DataAnnotations.ValidationResult>? validationResults, bool validateAllProperties, System.Threading.CancellationToken cancellationToken = default);
[<System.Diagnostics.CodeAnalysis.RequiresUnreferencedCode("Constructing a ValidationContext without a display name is not trim-safe because it uses reflection to discover the type of the instance being validated in order to resolve the DisplayNameAttribute when a display name is not provided.")>]
static member TryValidateObjectAsync : obj * System.ComponentModel.DataAnnotations.ValidationContext * System.Collections.Generic.ICollection<System.ComponentModel.DataAnnotations.ValidationResult> * bool * System.Threading.CancellationToken -> System.Threading.Tasks.Task<bool>
Public Shared Function TryValidateObjectAsync (instance As Object, validationContext As ValidationContext, validationResults As ICollection(Of ValidationResult), validateAllProperties As Boolean, Optional cancellationToken As CancellationToken = Nothing) As Task(Of Boolean)
参数
- instance
- Object
要测试的对象实例。 它不能 null。
- validationContext
- ValidationContext
描述要验证和提供验证器的服务和上下文的对象。
- validationResults
- ICollection<ValidationResult>
作为实例接收验证失败 ValidationResult 的可选集合。
- validateAllProperties
- Boolean
如果 true,还计算对象的所有属性(此过程不会递归属性的属性)。
- cancellationToken
- CancellationToken
等待任务完成时观察的 A CancellationToken 。
返回
Task<TResult>也就是说true,如果对象有效,false则遇到任何验证错误。
- 属性
例外
何时 instance 为 null。
何时 validationContext 为 null。
何时instance与 on validationContext不匹配ObjectInstance。
验证操作已取消。
注解
此方法计算附加到对象实例类型的所有 ValidationAttribute对象。 它还检查以确保已设置标记 RequiredAttribute 的所有属性。
true如果是validateAllProperties,此方法还会ValidationAttribute评估此对象的所有即时属性的 s。 此过程不是递归的。
如果为true时validateAllProperties,将按顺序为每个属性启动验证。 属性的同步属性在等待该属性的异步属性之前运行。 正在进行的异步验证可能与其他属性的异步验证重叠。
如果为null,validationResults验证在第一个属性错误(跨属性短路)后停止。 其他属性上的任何正在进行的异步验证程序将协同取消。 当为非 null 时 validationResults ,将收集所有属性并收集所有错误。
返回 Task<TResult> 与标准异步组合模式(例如 Task.WhenAll 和 Task.WhenAny)的互操作性。
验证分三个阶段运行:属性验证、类型级属性验证和对象级验证。 仅当前面的阶段成功时,每个阶段才会运行。 对象级验证更倾向于在实现时调用 ValidateAsync(ValidationContext, CancellationToken) ,而 Validate(ValidationContext) 对于仅实现同步接口的对象,则作为回退。 它不调用这两者。