C#反射动态校验属性类型信息

不多说,直接上代码

主类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ReflectionProj
{
    class Program
    {
        static void Main(string[] args)
        {
            Person p1 = new Person();
            p1.Name = "ABC";
            p1.Age = 30;
            p1.Salary = 8000.50;

            Person p2 = new Person();
            p2.Name = "DEFG";
            p2.Age = 9000000;
            p2.Salary = 7000.50;

            CheckResult c1 = Utils.CheckItemType(p1);
            Console.WriteLine(c1.ItemName + ":" + c1.IsTypeRight.ToString());
            CheckResult c2 = Utils.CheckItemType(p2);
            Console.WriteLine(c2.ItemName + ":" + c2.IsTypeRight.ToString());
            Console.WriteLine("Main OK");
            Console.Read();

        }


    }
}

Person类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace ReflectionProj
{
    public class Person
    {
        [ItemInfo("Name","string",4,0)]
        public string Name { get; set; }
        [ItemInfo("Age", "int", 3, 0)]
        public int Age { get; set; }
        [ItemInfo("Salary", "double", 10, 2)]
        public double Salary { get; set; }
    }
}

ItemInfo类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Reflection;

namespace ReflectionProj
{
   public  class ItemInfo: Attribute
    {
        public string ItemName { get; set; }
        public string DataType { get; set; }
        public int Length { get; set; }
        public int Jingdu { get; set; }

        public ItemInfo(string _itemName, string _dataType,int _length,int _jingdu)
        {
            ItemName = _itemName;
            DataType = _dataType;
            Length = _length;
            Jingdu = _jingdu;
        }
    }
}

Utils类:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace ReflectionProj
{
    public class CheckResult
    {
        public string ItemName { get; set; }
        public bool IsTypeRight { get; set; }

        public CheckResult()
        {
            ItemName = "AllItem";
            IsTypeRight = true;
        }
    }
    public static class Utils
    {

        public static CheckResult CheckItemType<T>(T obj)
        {
            CheckResult checkResult = new CheckResult();
            bool valCheckRes;
            Type type = obj.GetType();
            foreach (PropertyInfo p in type.GetProperties())
            {
                object val = p.GetValue(obj);
                Type pType = p.GetType();
                foreach (object a in p.GetCustomAttributes())
                {
                    ItemInfo info = (ItemInfo)a;
                    if (null != info)
                    {
                        valCheckRes = CheckValueType(val, info.DataType, info.Length, info.Jingdu);
                        if (!valCheckRes)
                        {
                            checkResult.ItemName = info.ItemName;
                            checkResult.IsTypeRight = valCheckRes;
                            return checkResult;
                        }
                    }
                }
            }
            return checkResult;
        }

        public static bool CheckValueType<T>(T itemValue, string typeName, int length, int jingdu = 0)
        {
            bool valCheckRes = false;
            string strTmp = ConvertToString(itemValue);
            switch (typeName)
            {
                case "string":
                    if (!string.IsNullOrEmpty(strTmp)
                        && strTmp.Length <= length)
                    {
                        valCheckRes = true;
                    }
                    else
                    {
                        return false;
                    }
                    break;
                case "int":
                    int tmp;
                    if (int.TryParse(strTmp, out tmp) 
                        && strTmp.Length<=length)
                    {
                        valCheckRes = true;
                    }
                    else
                    {
                        return false;
                    }
                    break;
                case "double":
                    valCheckRes = true;
                    break;
                default:
                    break;
            }
            return valCheckRes;
        }

        private static string ConvertToString<T>(T val)
        {
            string str = string.Empty;
            try
            {
                if (val != null)
                {
                    str = val.ToString();
                }
                else
                {
                    str = string.Empty;
                }
                return str;
            }
            catch (Exception)
            {
                throw;
            }

        }
    }
}
发布了51 篇原创文章 · 获赞 4 · 访问量 4216

猜你喜欢

转载自blog.csdn.net/songjian1104/article/details/104041122
今日推荐