本文整理汇总了C#中System.Web.Http.Metadata.ModelMetadata.GetValidators方法的典型用法代码示例。如果您正苦于以下问题:C# ModelMetadata.GetValidators方法的具体用法?C# ModelMetadata.GetValidators怎么用?C# ModelMetadata.GetValidators使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Web.Http.Metadata.ModelMetadata
的用法示例。
在下文中一共展示了ModelMetadata.GetValidators方法的6个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Validate
public override IEnumerable<ModelValidationResult> Validate(ModelMetadata metadata, object container)
{
bool propertiesValid = true;
foreach (ModelMetadata propertyMetadata in metadata.Properties)
{
foreach (ModelValidator propertyValidator in propertyMetadata.GetValidators(ValidatorProviders))
{
foreach (ModelValidationResult propertyResult in propertyValidator.Validate(metadata, container))
{
propertiesValid = false;
yield return new ModelValidationResult
{
MemberName = ModelBindingHelper.CreatePropertyModelName(propertyMetadata.PropertyName, propertyResult.MemberName),
Message = propertyResult.Message
};
}
}
}
if (propertiesValid)
{
foreach (ModelValidator typeValidator in metadata.GetValidators(ValidatorProviders))
{
foreach (ModelValidationResult typeResult in typeValidator.Validate(metadata, container))
{
yield return typeResult;
}
}
}
}
示例2: GetValidators
public IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata)
{
// If metadata is for a property then containerType != null && propertyName != null
// If metadata is for a type then containerType == null && propertyName == null, so we have to use modelType for the cache key.
Type typeForCache = metadata.ContainerType ?? metadata.ModelType;
Tuple<Type, string> cacheKey = Tuple.Create(typeForCache, metadata.PropertyName);
ModelValidator[] validators;
if (!TryGetValue(cacheKey, out validators))
{
_cacheLock.EnterWriteLock();
try
{
// Check the cache again in case the value was computed and added to the cache while we were waiting on the write lock
if (!_validatorCache.TryGetValue(cacheKey, out validators))
{
// Compute validators
validators = metadata.GetValidators(_validatorProviders.Value).ToArray();
_validatorCache.Add(cacheKey, validators);
}
}
finally
{
_cacheLock.ExitWriteLock();
}
}
return validators;
}
示例3: GetValidators
public ModelValidator[] GetValidators(ModelMetadata metadata)
{
ModelValidator[] validators;
if (!_validatorCache.TryGetValue(metadata.CacheKey, out validators))
{
// Compute validators
// There are no side-effects if the same validators are created more than once
validators = metadata.GetValidators(_validatorProviders.Value).ToArray();
_validatorCache.TryAdd(metadata.CacheKey, validators);
}
return validators;
}
示例4: GetValidators
public IEnumerable<ModelValidator> GetValidators(ModelMetadata metadata)
{
// If metadata is for a property then containerType != null && propertyName != null
// If metadata is for a type then containerType == null && propertyName == null, so we have to use modelType for the cache key.
Type typeForCache = metadata.ContainerType ?? metadata.ModelType;
Tuple<Type, string> cacheKey = Tuple.Create(typeForCache, metadata.PropertyName);
ModelValidator[] validators;
if (!_validatorCache.TryGetValue(cacheKey, out validators))
{
// Compute validators
// There are no side-effects if the same validators are created more than once
validators = metadata.GetValidators(_validatorProviders.Value).ToArray();
_validatorCache.TryAdd(cacheKey, validators);
}
return validators;
}
示例5: ShallowValidate
// Validates a single node (not including children)
// Returns true if validation passes successfully
private static bool ShallowValidate(ModelMetadata metadata, ValidationContext validationContext, object container, string key)
{
bool isValid = true;
foreach (ModelValidator validator in metadata.GetValidators(validationContext.ValidatorProviders))
{
foreach (ModelValidationResult error in validator.Validate(container))
{
validationContext.ModelState.AddModelError(key, error.Message);
isValid = false;
}
}
return isValid;
}
示例6: GetValidators
private IEnumerable<ModelValidator> GetValidators(IEnumerable<ModelValidatorProvider> validatorProviders, ModelMetadata metadata)
{
// If metadata is for a property then containerType != null && propertyName != null
// If metadata is for a type then containerType == null && propertyName == null, so we have to use modelType for the cache key.
Type typeForCache = metadata.ContainerType ?? metadata.ModelType;
Tuple<Type, string> cacheKey = Tuple.Create(typeForCache, metadata.PropertyName);
// This retrieval is implemented as a TryGetValue/TryAdd instead of a GetOrAdd to avoid the performance cost of creating delegates
IEnumerable<ModelValidator> validators;
if (!_validatorCache.TryGetValue(cacheKey, out validators))
{
validators = metadata.GetValidators(validatorProviders).ToArray();
_validatorCache.TryAdd(cacheKey, validators);
}
return validators;
}