C#_Model反射_数据校验

首次发布:2018-08-05 23:28

网络时代,对客户端传来的数据_数据校验是很必要的

文件结构图


以下是TestModel.cs 里面的代码《Model》

using luofenming.com.Common;

namespace luofenming.com.Model
{
    public class TestModel
    {
        public int ID { get; set; }
        [LenghtAttribute(5,15)]
        public string Name { get; set; }
        public long QQ { get; set; }
        [EmailAttribute]
        public string Email { get; set; }
    }
}

以下是LenghtAttribute.cs里面的代码 《校验特性字符长度》
namespace luofenming.com.Common
{
    public class LenghtAttribute : BaseAttribute
    {
        private int _minLenght = 0;
        private int _maxLenght = 0;
        public LenghtAttribute(int minLenght,int maxLenght)
        {
            this._minLenght = minLenght;
            this._maxLenght = maxLenght;
        } 
        public override bool Validate(object oValue)
        {
            if (oValue != null)
            {
                return oValue.ToString().Length >= _minLenght && oValue.ToString().Length <= _maxLenght;
            }
             else
            {
               if (_minLenght > 0)
               {
                 return false;
                }
             }
            return true;
        }
    }
}

以下是EmailAttribute.cs里面的代码《校验邮箱特性 》
using System.Text.RegularExpressions;

namespace luofenming.com.Common
{
    public  class EmailAttribute : BaseAttribute
    {
        public override bool Validate(object oValue)
        {
            if (oValue != null)
            {
            Regex r = new Regex("^\\s*([A-Za-z0-9_-]+(\\.\\w+)*@(\\w+\\.)+\\w{2,5})\\s*$");
            return r.IsMatch(oValue.ToString());
            }
            return true;

        }
    }
}

以下是BaseAttribute.cs里面的代码《校验特性基类》

namespace luofenming.com.Common
{
    public abstract class BaseAttribute:Attribute
    {
        public abstract bool Validate(object oValue);
    }
}

以下是AttributeExtend.cs里面的代码
using System;
using System.Reflection;

namespace luofenming.com.Common
{
    public static class AttributeExtend
    {
        public static bool Validate<T>(T t)
        {
            Type type = t.GetType();
            foreach (PropertyInfo prop in type.GetProperties())//pr
            {

                if (prop.IsDefined(typeof(BaseAttribute), true))
                {
                    object oValue = prop.GetValue(t, null);

                    foreach (BaseAttribute iTemp in prop.GetCustomAttributes(typeof(BaseAttribute), true))//获取字段所有的特性
                    {
                        if (!iTemp.Validate(oValue))
                        {
                            return false;
                        }
                    }
                }
            }
            return true;
        }
    }
}

以下是调用校验方法
        private void button1_Click(object sender, EventArgs e)
        {
            TestModel model = new TestModel()
            {
                ID = 10000,
                Name = "罗分明是谁",
                QQ = 78630559,
                Email = "123@luofenming.com"
            };
            bool b = AttributeExtend.Validate<TestModel>(model);
        }