csharp 自定义需要可能与dict一起使用的验证器。

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了csharp 自定义需要可能与dict一起使用的验证器。相关的知识,希望对你有一定的参考价值。

using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http.Filters;
using System.Web.Http.Controllers;
using System.ComponentModel.DataAnnotations;

namespace Dovetail.Novaerus.Argus.WebUI.Infrastructure.Validation
{
    // http://stackoverflow.com/questions/14836248/passing-custom-parameters-to-validationattribute
    public class RequireDictItemAttribute : RequiredAttribute
    {
        string[] _args;

        public RequireDictItemAttribute(params string[] args)
        {
            _args = args;
        }

        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            if (_args.Contains((string)value))
                return ValidationResult.Success;

            
            var id = validationContext.ObjectInstance.GetType()
                        .GetProperty("Id")
                        .GetValue(validationContext.ObjectInstance, null);
            var key = new[]
            {
                id.ToString()
            };
            return new ValidationResult("Invalid value.", key);
        }
    }
}

以上是关于csharp 自定义需要可能与dict一起使用的验证器。的主要内容,如果未能解决你的问题,请参考以下文章

如何将 py_func 与返回 dict 的函数一起使用

将 Gradle 与自定义 Ivy 存储库一起使用

将 Firebase 身份验证与自定义提供程序一起使用

与 qtwebkit 请求一起发送自定义标头

SQOOP 可以与自定义 libpath 一起使用吗?

csharp 父子相关的自引用表数据绑定在一起并相应地排序(使用递归调用).cs