当前位置: 首页>>代码示例>>C#>>正文


C# ModelMetadata.GetValidators方法代码示例

本文整理汇总了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;
                        }
                    }
                }
            }
开发者ID:KevMoore,项目名称:aspnetwebstack,代码行数:31,代码来源:ModelValidator.cs

示例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;
        }
开发者ID:chrissimon-au,项目名称:aspnetwebstack,代码行数:28,代码来源:ModelValidatorCache.cs

示例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;
 }
开发者ID:huangw-t,项目名称:aspnetwebstack,代码行数:12,代码来源:ModelValidatorCache.cs

示例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;
        }
开发者ID:brianly,项目名称:aspnetwebstack,代码行数:17,代码来源:ModelValidatorCache.cs

示例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;
 }
开发者ID:sanyaade-mobiledev,项目名称:aspnetwebstack-1,代码行数:15,代码来源:DefaultBodyModelValidator.cs

示例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;
        }
开发者ID:JokerMisfits,项目名称:linux-packaging-mono,代码行数:16,代码来源:DefaultBodyModelValidator.cs


注:本文中的System.Web.Http.Metadata.ModelMetadata.GetValidators方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。