zoukankan      html  css  js  c++  java
  • asp.net 后端验证

    using EntryRegistration.Filters;
    using EntryRegistration.Models.Entity;
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Web;
    
    namespace EntryRegistration.Models
    {
        public class Check
        {
            /// <summary>
            /// 检查方法,支持泛型
            /// </summary>
            /// <typeparam name="T"></typeparam>
            /// <param name="instance"></param>
            /// <returns></returns>
            public static CheckResult CheckRequire<T>(T instance)
            {
    
                //初始化验证结果
                CheckResult result = new CheckResult()
                {
                    isSuccess = true,
                    msg = "验证OK"
                };
                //获取T类的属性
                Type t = typeof(T);
                var propertyInfos = t.GetProperties();
    
                //遍历属性
                foreach (var propertyInfo in propertyInfos)
                {
                    //检查属性是否标记了特性
                    RequireAttribute attribute = (RequireAttribute)Attribute.GetCustomAttribute(propertyInfo, typeof(RequireAttribute));
    
                    //没标记,直接跳过
                    if (attribute == null)
                    {
                        continue;
                    }
    
                    //获取该属性的值
                    var value = propertyInfo.GetValue(instance);
                    //获取属性的数据类型
                    if (typeof(string) == propertyInfo.PropertyType)
                    {
                        if (string.IsNullOrEmpty((string)value) && attribute.IsRequire)
                        {
                            result.isSuccess = false;
                            result.msg = propertyInfo.Name + "不能为空";
                            return result;
                        }
    
                    }
                    else if (typeof(int?) == propertyInfo.PropertyType)
                    {
                        if (value == null && attribute.IsRequire)
                        {
                            result.isSuccess = false;
                            result.msg = propertyInfo.Name + "不能为空";
                            return result;
                        }
                    }
                    else if (typeof(Guid) == propertyInfo.PropertyType)
                    {
                        if (Guid.Parse(value.ToString()) == Guid.Empty && attribute.IsRequire)
                        {
                            result.isSuccess = false;
                            result.msg = propertyInfo.Name + "不能为空";
                            return result;
                        }
                    }
                }
    
                return result;
            }
        }
    }

     //RequireAttribute.cs

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    
    namespace EntryRegistration.Filters
    {
        public class RequireAttribute : Attribute
        {
    
            public bool IsRequire { get; }
    
            /// <summary>
            /// 构造函数
            /// </summary>
            /// <param name="isRequire"></param>
            public RequireAttribute(bool IsRequire)
            {
                this.IsRequire = IsRequire;
            }
        }
    }
  • 相关阅读:
    elk工作原理
    nginx 引入外部文件
    3.1.2 视图实例演示-登录页面
    通信架构
    JAX-WS HandlerChain使用详解
    改变规则可以,前提是得有本事——北漂18年(64)
    An internal error occurred during:"Update Installed JREs".java.lang.NullPointerException
    jquery.mobile-1.4.5.min.js:3 Uncaught TypeError: Cannot set property 'mobile' of undefined
    大数据时代,百货行业信息化将如何变革?
    17.2?Replication Implementation 复制实施:
  • 原文地址:https://www.cnblogs.com/gaocong/p/7298824.html
Copyright © 2011-2022 走看看