zoukankan      html  css  js  c++  java
  • 泛型

    1.枚举获取特性,转换值

    using System;
    using System.Reflection;
    
    namespace MyAttribute
    {
        public static class HelloExtesion
        {
            public static string GetStr(this Enum e)
            {
                Type type = typeof(UserState);
                FieldInfo p = type.GetField(e.ToString());
                if (p.IsDefined(typeof(CSAttribute), true))
                {
                    CSAttribute cs = p.GetCustomAttribute(typeof(CSAttribute)) as CSAttribute;
                    return cs.Name;
                }
                else
                {
                    return e.ToString();
                }
            }
        }
        public enum UserState
        {
            [CS("富人")]
            Rich = 1,
            [CS("一般人")]
            Normal = 2,
            //[CS("穷人")]
            Poor = 3,
        }
        public class CSAttribute : Attribute
        {
            public string Name { set; get; }
            public CSAttribute(string Name)
            {
                this.Name = Name;
            }
        }
    }
    View Code

    2.特性验证字段格式

    using System;
    using System.Reflection;
    
    namespace MyAttribute
    {
        public static class DataValidate
        {
            public static void Validate<T>(this T t)
            {
                Type type = typeof(T);
                foreach (var item in type.GetProperties())
                {
                    if (item.IsDefined(typeof(AgeAttribute), true))
                    {
                        AgeAttribute ageAttribute = item.GetCustomAttribute(typeof(AgeAttribute)) as AgeAttribute;
                        if (!ageAttribute.Validate(item.GetValue(t)))
                        {
                            throw new Exception("年龄格式不正确");
                        }
                    }
                    if (item.IsDefined(typeof(QQAttribute), true))
                    {
                        QQAttribute qqAttribute = item.GetCustomAttribute(typeof(QQAttribute)) as QQAttribute;
                        if (!qqAttribute.Validate(item.GetValue(t)))
                        {
                            throw new Exception("QQ格式不正确");
                        }
                    }
                }
            }
        }
        public class Yun
        {
            public int Id { set; get; }
    
            [AgeAttribute(0, 100)]
            public int Age { set; get; }
            [QQAttribute(10001, 9999999999)]
            public long QQ { set; get; }
            public string Email { set; get; }
        }
    
        public class QQAttribute : Attribute
        {
            public long _min { set; get; }
            public long _max { set; get; }
            public QQAttribute(long min, long max)
            {
                this._min = min;
                this._max = max;
            }
            public bool Validate(object obj)
            {
                if (obj != null)
                {
                    if (long.TryParse(obj.ToString(), out long _Result))
                    {
                        return _Result >= _min && _Result <= _max;
                    }
                }
                return false;
            }
        }
    
        public class AgeAttribute : Attribute
        {
            public int _min { set; get; }
            public int _max { set; get; }
            public AgeAttribute(int min, int max)
            {
                this._min = min;
                this._max = max;
            }
            public bool Validate(object obj)
            {
                if (obj != null)
                {
                    if (Int32.TryParse(obj.ToString(), out int _Result))
                    {
                        return _Result >= _min && _Result <= _max;
                    }
                }
                return false;
            }
        }
    }
    DataValidate
  • 相关阅读:
    离散数学期中复习
    计算机组成原理实验_算术逻辑运算器的实现
    数值分析第一章插值方法
    数值分析绪论
    数值分析第三章 常微分方程的差分方法
    数值分析第二章 数值积分
    数据库删除信息后,再次加入信息ID不再从1开始的解决办法
    Codeforces Round #670 (Div. 2)(树的重心,dfs求子树大小)
    Codeforces Round #670 (Div. 2)B. Maximum Product(5个数乘积最大)
    Codeforces Round #668 (Div. 2)A->C
  • 原文地址:https://www.cnblogs.com/Jacob-Wu/p/9291776.html
Copyright © 2011-2022 走看看