zoukankan      html  css  js  c++  java
  • c#通过反射获取类上的自定义特性

    c#通过反射获取类上的自定义特性

    本文转载:http://www.cnblogs.com/jeffwongishandsome/archive/2009/11/18/1602825.html

    下面这个是笔者在以前的一个项目中用到的。当时是为了在导出excel报表的时侯,通过自定义特性,包含一些可配置的特性在里面。具体的操作excel不是本文重点,本文不会多做说明。下面只写个示例,简单说明一下如何通过反射获取自定义特性。示例只在类和属性上使用了自定义特性。读者可以按照实际的项目需求,合理使用自定义特性。

    1、实现实体自定义特性,继承自Attribute类

    复制代码
       ///<summary>     /// 自定义特性 属性或者类可用  支持继承     ///</summary>    [AttributeUsage(AttributeTargets.Property | AttributeTargets.Class, Inherited =true)]     publicclass EnitityMappingAttribute : Attribute     {         privatestring tableName;         ///<summary>         /// 实体实际对应的表名         ///</summary>        publicstring TableName         {             get { return tableName; }             set { tableName = value; }         }
           
    privatestring columnName;         ///<summary>         /// 中文列名         ///</summary>        publicstring ColumnName         {             get { return columnName; }             set { columnName = value; }         }     }
    复制代码

    注释中我已经写的很清楚,自定义特性中的属性一个是实体实际对应的数据库表名,一个是对应的中文列名称。 2、在实体中使用自定义特性

    复制代码
    ///<summary>     /// 会员 ,实际的表名叫MemberInfo,并不是和实体名一致     ///</summary>    [EnitityMapping(TableName="MemberInfo")]     publicclass Member     {         privateint id;         [EnitityMapping(ColumnName="关键字")]         publicint Id         {             get { return id; }             set { id = value; }         }
           
    privatestring userName;         [EnitityMapping(ColumnName ="会员注册名")]         publicstring UserName         {             get { return userName; }             set { userName = value; }         }
           
    privatestring realName;         [EnitityMapping(ColumnName ="会员真实名")]         publicstring RealName         {             get { return realName; }             set { realName = value; }         }
           
    privatebool isActive;         ///<summary>         /// 是否活跃  没有附加自定义属性         ///</summary>        publicbool IsActive         {             get { return isActive; }             set { isActive = value; }         }     }
    复制代码

    3、显示自定义特性

    复制代码
       class Program     {         ///<summary>         /// 通过反射取自定义属性         ///</summary>         ///<typeparam name="T"></typeparam>        privatestaticvoid DisplaySelfAttribute<T>() where T:class ,new()         {             string tableName =string.Empty;             List<string> listColumnName =new List<string>();             Type objType =typeof(T);             //取属性上的自定义特性            foreach (PropertyInfo propInfo in objType.GetProperties())             {                 object[] objAttrs = propInfo.GetCustomAttributes(typeof(EnitityMappingAttribute), true);                 if (objAttrs.Length>0)                 {                     EnitityMappingAttribute attr = objAttrs[0] as EnitityMappingAttribute;                     if (attr !=null)                     {                         listColumnName.Add(attr.ColumnName); //列名                    }                 }             }
               
    //取类上的自定义特性            object[] objs = objType.GetCustomAttributes(typeof(EnitityMappingAttribute), true);             foreach (object obj in objs)             {                 EnitityMappingAttribute attr = obj as EnitityMappingAttribute;                 if (attr !=null)                 {
                        tableName
    = attr.TableName;//表名只有获取一次                    break;                 }             }             if (string.IsNullOrEmpty(tableName))             {                 tableName = objType.Name;             }             Console.WriteLine(string.Format("The tablename of the entity is:{0} ", tableName));             if (listColumnName.Count >0)             {                 Console.WriteLine("The columns of the table are as follows:");                 foreach (string item in listColumnName)                 {                     Console.WriteLine(item);                 }             }         }
           
    staticvoid Main(string[] args)         {             DisplaySelfAttribute<Member>(); //显示结果            Console.ReadLine();         }     }
    复制代码
  • 相关阅读:
    112、TensorFlow初始化变量
    111、TensorFlow 初始化变量
    110、TensorFlow张量值的计算
    109、TensorFlow计算张量的值
    108、TensorFlow 类型转换
    107、TensorFlow变量(三)
    106、TensorFlow变量 (二) reshape
    105、TensorFlow的变量(一)
    104、Tensorflow 的变量重用
    103、Linux 编译 Kaldi 语音识别工具
  • 原文地址:https://www.cnblogs.com/wangp2012/p/3181273.html
Copyright © 2011-2022 走看看