zoukankan      html  css  js  c++  java
  • C#enum使用Attribute求字段名

    用到了一些反射:(自己看吧)

    public enum UserState
        {
            /// <summary>
            /// 正常
            /// </summary>
            [Remark("正常")]
            Normal = 0,//左边是字段名称  右边是数据库值   哪里放描述?  注释
            /// <summary>
            /// 冻结
            /// </summary>
            [Remark("冻结")]
            Frozen = 1,
            /// <summary>
            /// 删除
            /// </summary>
            //[Remark("删除")]
            Deleted = 2
        }
        public class RemarkAttribute : Attribute
        {
            public RemarkAttribute(string remark)
            {
                this._Remark = remark;
            }
            private string _Remark = null;
            public string GetRemark()
            {
                return this._Remark;
            }
        }
    
        public static class RemarkExtension
        {
            public static string GetRemark(this Enum value)
            {
                Type type = value.GetType();
                FieldInfo field = type.GetField(value.ToString());
                if (field.IsDefined(typeof(RemarkAttribute), true))
                {
                    RemarkAttribute attribute = (RemarkAttribute)field.GetCustomAttribute(typeof(RemarkAttribute), true);
                    return attribute.GetRemark();
                }
                else
                {
                    return value.ToString();
                }
            }
                
        }
    
        public abstract class AbstractValidateAttribute : Attribute
        {
            public abstract bool Validate(object oValue);
        }
        public class LongValidateAttribute : AbstractValidateAttribute
        {
            private long _lMin = 0;
            private long _lMax = 0;
    
            public LongValidateAttribute(long lMin, long LMax)
            {
                this._lMin = lMin;
                this._lMax = LMax;
            }
            public override bool Validate(object oValue)
            {
                return this._lMin < (long)oValue && (long)oValue < this._lMax;
            }
        }
        public class RequirdValidateAttribute : AbstractValidateAttribute
        {
            public override bool Validate(object oValue)
            {
                return oValue != null;
            }
        }
        public class DataValidate
        {
            public static bool Validate<T>(T t)
            {
                Type type = t.GetType();
                bool result = true;
                foreach (var prop in type.GetProperties())
                {
                    if (prop.IsDefined(typeof(AbstractValidateAttribute), true))
                    {
                        object item = prop.GetCustomAttributes(typeof(AbstractValidateAttribute), true)[0];
                        AbstractValidateAttribute attribute = item as AbstractValidateAttribute;
                        if (!attribute.Validate(prop.GetValue(t)))
                        {
                            result = false;
                            break;
                        }
                    }
                }
                return result;
            }
        }
    

      

  • 相关阅读:
    IIS浏览显示目录
    图解NuGet的安装和使用
    未能找到类型或命名空间名称“DbContext”
    IIS报错:未将对象引用设置到对象的实例
    最新11位手机号正则表达式
    Sql Server连表查询字段为null
    sql server 表连接
    2019用卡提额攻略
    win10,7 80端口被占用的检测和解决方法
    SAP之RFC_READ_TABLE
  • 原文地址:https://www.cnblogs.com/sunliyuan/p/10248895.html
Copyright © 2011-2022 走看看