NET 基于ValidationContext和反射,调用过滤器校验参数

业务背景:

页面可视化批量操作数据,支持添加修改删除等操作,某天耐不住客户拍脑袋,要求把该功能改成文件上传
文件流没办法使用之前的【过滤器校验参数】逻辑,重新写容易出毛病【主要是上头懒得折腾】,就想出了这个骚操作
PS1:以前流程为【浏览器选择文件 / 浏览器展示&操作数据 / 调用批量上传数据接口】
PS2:现在流程为【浏览器选择文件 / 调用上传接口】

1.添加引用[展开项目 / 选中“引用” / 右键 / 添加引用 / 程序集]:System.ComponentModel.DataAnnotations

2.校验核心代码

复制代码
                var itemModel = dtoList[i];
                var context = new System.ComponentModel.DataAnnotationsValidationContext(itemModel);

                // 存在错误信息的字段集合
                var itemErrorFieldNameList = new List<string>();
                // 循环属性
                foreach (var itemMapping in mappingList)
                {
                    context.MemberName = itemMapping.RealName;
                    context.DisplayName = itemMapping.RealName;
                    var itemValue = entityType.GetProperty(itemMapping.RealName).GetValue(itemModel);
                    // 循环属性的特性
                    foreach (var itemObject in entityType.GetProperty(itemMapping.RealName).GetCustomAttributes(false))
                    {
                        ValidationAttribute validationAttribute = null;
                        if (itemObject is Filters.CustomRequiredAttribute)
                            validationAttribute = itemObject as Filters.CustomRequiredAttribute;
                        else if (itemObject is Filters.CustomNotSupportAttribute)
                            validationAttribute = itemObject as Filters.CustomNotSupportAttribute;
                        else if (itemObject is Filters.CustomRequiredDateAttribute)
                            validationAttribute = itemObject as Filters.CustomRequiredDateAttribute;
                        if (validationAttribute == null) continue;
                        var itemBoo = validationAttribute.GetValidationResult(itemValue, context);
                        if (itemBoo != null) itemErrorFieldNameList.Add(itemBoo.ErrorMessage);
                    }
                }
                if (!itemErrorFieldNameList.IsNullOrEmpty()) errorFieldNameList.Add($"第 {i + 2} 行字段【{itemErrorFieldNameList.Distinct().Join("/")}】的数据有问题!");
复制代码

3.Mapping

4.CustomRequiredAttribute

复制代码
    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Field | AttributeTargets.Parameter, AllowMultiple = false)]
    public class CustomRequiredAttribute : ValidationAttribute
    {
        protected override ValidationResult IsValid(object value, ValidationContext validationContext)
        {
            var validPropertityDesc = validationContext.DisplayName;
            if (!value.IsValid())
            {
                return new ValidationResult(validPropertityDesc);
            }
            return ValidationResult.Success;
        }
    }
复制代码

 

posted @   Robot-Blog  阅读(106)  评论(0编辑  收藏  举报
编辑推荐:
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
点击右上角即可分享
微信分享提示