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;
            }
        }
    }
  • 相关阅读:
    基本MVVM 和 ICommand用法举例(转)
    WPF C# 命令的运行机制
    628. Maximum Product of Three Numbers
    605. Can Place Flowers
    581. Shortest Unsorted Continuous Subarray
    152. Maximum Product Subarray
    216. Combination Sum III
    448. Find All Numbers Disappeared in an Array
    268. Missing Number
    414. Third Maximum Number
  • 原文地址:https://www.cnblogs.com/gaocong/p/7298824.html
Copyright © 2011-2022 走看看