zoukankan      html  css  js  c++  java
  • .net 特性 Attribute

    public sealed class RemarkAttribute : Attribute
        {
            public string Remark { get; set; }
    
            // 构造函数
            public RemarkAttribute(string remark)
            {
                this.Remark = remark;
            }
    
            /// <summary>
            /// 获取枚举备注属性
            /// </summary>
            /// <param name="_enum">枚举类参数</param>
            /// <returns>String</returns>
            public static string GetEnumRemark(Enum _enum)
            {
                string result = string.Empty;
                Type type = _enum.GetType();
                FieldInfo fd = type.GetField(_enum.ToString());
                if (fd != null)
                {
                    object[] attrs = fd.GetCustomAttributes(typeof(RemarkAttribute), false);
                    foreach (RemarkAttribute attr in attrs)
                        result = attr.Remark;
                }
                return result;
            }
    
            /// <summary>
            /// 下拉列表绑定枚举属性值
            /// </summary>
            /// <param name="ddl">DropDownList</param>
            /// <param name="_enum">枚举</param>
            public static void DataBind(DropDownList ddl, Type _enum)
            {
                List<StructRemark> list = GetStructRemarkList(_enum);
                foreach (StructRemark item in list)
                {
                    ListItem listItem = new ListItem();
                    listItem = new ListItem(item.FieldText, item.FieldValue.ToString());
                    ddl.Items.Add(listItem);
                }
            }
    
            /// <summary>
            /// 下拉列表绑定枚举属性值(有默认项)
            /// </summary>
            /// <param name="ddl">DropDownList</param>
            /// <param name="_enum">枚举</param>
            public static void DataBind(DropDownList ddl, Type _enum, string firstValue, string firstText)
            {
                List<StructRemark> list = GetStructRemarkList(_enum);
                foreach (StructRemark item in list)
                {
                    ListItem listItem = new ListItem();
                    listItem = new ListItem(item.FieldText, item.FieldValue.ToString());
                    ddl.Items.Add(listItem);
                }
                ddl.Items.Insert(0, new ListItem(firstText, firstValue));
            }
    
            /// <summary>
            /// 把枚举转换成DataTable
            /// </summary>
            /// <param name="_enum">枚举</param>
            public static DataTable GetDataTable(Type _enum)
            {
                DataTable dt = new DataTable();
                DataColumn dc = null;
                dc = dt.Columns.Add("Value", Type.GetType("System.Int32"));
                dc = dt.Columns.Add("Text", Type.GetType("System.String"));
                DataRow dr = null;
                List<StructRemark> list = GetStructRemarkList(_enum);
                foreach (StructRemark item in list)
                {
                    dr = dt.NewRow();
                    dr["Value"] = item.FieldValue;
                    dr["Text"] = item.FieldText;
                    dt.Rows.Add(dr);
                }
                return dt;
            }
    
            /// <summary>
            /// 获取枚举备注属性集
            /// </summary>
            /// <param name="_enum">枚举</param>
            /// <returns>枚举属性结构</returns>
            public static List<StructRemark> GetStructRemarkList(Type _enum)
            {
                List<StructRemark> list = new List<StructRemark>();
                StructRemark model = new StructRemark();
                foreach (int value in Enum.GetValues(_enum))
                {
                    model.FieldValue = value;
                    FieldInfo fd = _enum.GetField(Enum.GetName(_enum, value));
                    string name = string.Empty;
                    if (fd == null)
                        name = "";
                    object[] attrs = fd.GetCustomAttributes(typeof(RemarkAttribute), false);
                    foreach (RemarkAttribute attr in attrs)
                    {
                        name = attr.Remark;
                    }
                    model.FieldText = name;
                    list.Add(model);
                }
                return list;
            }
    
            /// <summary>
            /// 枚举属性结构
            /// </summary>
            public struct StructRemark
            {
                public int FieldValue;//值
                public string FieldText;//键
            }
        }
    

    特性 Attribute:

    公共语言运行时允许添加类似关键字的描述声明,叫做attributes, 它对程序中的元素进行标注,如类型、字段、方法和属性等。Attributes和Microsoft .NET Framework文件的元数据保存在一起,可以用来向运行时描述你的代码,或者在程序运行的时候影响应用程序的行为。

    我们简单的总结为:定制特性attribute,本质上是一个类,其为目标元素提供关联附加信息,并在运行期以反射的方式来获取附加信息。具体的特性实现方法,在接下来的讨论中继续深入。

    自定义特性     public sealed class RemarkAttribute : Attribute  以Attribute  Remark则表示你的特性

  • 相关阅读:
    深入浅出 Redis client/server 交互流程
    VMware三种网络连接模式(转载)
    ARP 原理及攻击
    symbol lookup error:undefined symbol
    printf 颜色格式串"33[34;1m"
    运行openvas
    openvas 安装
    升级openssl 支持TLS1.2
    Windows登录密码明文获取器
    Linux字符串截取和处理命令 cut、printf、awk、sed、sort、wc
  • 原文地址:https://www.cnblogs.com/dragon-L/p/3261668.html
Copyright © 2011-2022 走看看