• C# 反射通过GetCustomAttributes方法,获得自定义特性


    http://blog.csdn.net/litao2/article/details/17633107

    使用反射访问: 自定义属性信息和对其进行操作的方法

    一、实例1

    1、代码:


     如:System.Attribute[] attrs=System.Attribute.GetCustomAttributes(typeof(FirstClass));

    [csharp] view plain copy
     
    1. namespace ConsoleApplication1  
    2. {  
    3.     class Program  
    4.     {  
    5.         static void Main(string[] args)  
    6.         {  
    7.   
    8.             PrintAuthorInfo(typeof(FirstClass));  
    9.             PrintAuthorInfo(typeof(SecondClass));  
    10.             PrintAuthorInfo(typeof(ThirdClass));  
    11.             Console.ReadKey();  
    12.         }  
    13.   
    14.         private static void PrintAuthorInfo(System.Type t)  
    15.         {  
    16.             System.Console.WriteLine(" 类型的 System.Type 对象是:{0}", t);  
    17.             System.Attribute[] attrs = System.Attribute.GetCustomAttributes(t);  //反射获得用户自定义属性  
    18.   
    19.             foreach (System.Attribute attr in attrs)  
    20.             {  
    21.                 if (attr is Author)  
    22.                 {  
    23.                     Author a = (Author)attr;  
    24.                     System.Console.WriteLine("   名称:{0}, 版本: {1:f}", a.GetName(), a.version);  
    25.                 }  
    26.             }  
    27.         }  
    28.     }  
    29.   
    30.   
    31.   
    32.     [System.AttributeUsage(System.AttributeTargets.Class |  
    33.                        System.AttributeTargets.Struct,  
    34.                        AllowMultiple = true)  
    35.     ]//自定义特性类(应用特性的程序元素(是类或结构),程序元素可以指定多个特性)  
    36.     public class Author : System.Attribute  
    37.     {  
    38.         string name;  
    39.         public double version;  
    40.   
    41.         public Author(string name)  
    42.         {  
    43.             this.name = name;  
    44.             version = 1.0;  // Default value  
    45.         }  
    46.   
    47.         public string GetName()  
    48.         {  
    49.             return name;  
    50.         }  
    51.     }  
    52.   
    53.     [Author("H. Ackerman")]  
    54.     public class FirstClass  
    55.     {  
    56.         // ...  
    57.     }  
    58.   
    59.     // No Author attribute  
    60.     public class SecondClass  
    61.     {  
    62.         // ...  
    63.     }  
    64.   
    65.     [Author("H. Ackerman"), Author("M. Knott", version = 2.0)]  
    66.     public class ThirdClass  
    67.     {  
    68.         //程序元素可以指定多个特性  
    69.     }  
    70.   
    71.   
    72. }  


     

    2、效果:

    二、实例2

    1、代码

    [csharp] view plain copy
     
    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.Linq.Expressions;  
    6. using System.Collections.Specialized;  
    7. using System.Reflection;  
    8. using System.Data.Linq.Mapping;  
    9. namespace ConsoleApplication1  
    10. {  
    11.     class Program  
    12.     {  
    13.         static void Main(string[] args)  
    14.         {  
    15.   
    16.             PropertyInfo[] propertys = typeof(FirstClass).GetProperties();//返回FirstClass的所有公共属性  
    17.             if (propertys != null && propertys.Length > 0)  
    18.             {  
    19.                 foreach (PropertyInfo p in propertys)  
    20.                 {  
    21.                     object[] objAttrs = p.GetCustomAttributes(typeof(ColumnAttribute), true);//获取自定义特性  
    22.                     //GetCustomAttributes(要搜索的特性类型,是否搜索该成员的继承链以查找这些特性)  
    23.                     if (objAttrs != null && objAttrs.Length > 0)  
    24.                     {  
    25.                         ColumnAttribute attr = objAttrs[0] as ColumnAttribute;  
    26.                         Console.WriteLine("自定义特性Name:"+p.Name+", 元数据:"+attr);  
    27.                     }  
    28.                 };  
    29.             }  
    30.             Console.ReadKey();  
    31.         }  
    32.   
    33.     }  
    34.   
    35.   
    36.   
    37.     public class FirstClass  
    38.     {  
    39.         private int _newsid = 0;  
    40.   
    41.         /// <summary>  
    42.         /// 主键  
    43.         /// </summary>          
    44.         [Column(Name = "NewsId", DbType = "int", IsPrimaryKey = true, CanBeNull = false, IsDbGenerated = true)]  
    45.         public int NewsId  
    46.         {  
    47.             get  
    48.             {  
    49.                 return this._newsid;  
    50.             }  
    51.             set  
    52.             {  
    53.                 this._newsid = value;  
    54.             }  
    55.         }  
    56.   
    57.         private string _newsimage = string.Empty;  
    58.   
    59.         /// <summary>  
    60.         /// 资讯标题图片  
    61.         /// </summary>          
    62.         [Column(Name = "NewsImage", DbType = "varchar", IsPrimaryKey = false, CanBeNull = false, IsDbGenerated = false)]  
    63.         public string NewsImage  
    64.         {  
    65.             get  
    66.             {  
    67.                 return this._newsimage;  
    68.             }  
    69.             set  
    70.             {  
    71.                 this._newsimage = value;  
    72.             }  
    73.         }  
    74.     }  
    75. }  


    2、效果

    其他:

    FullName(获得System.Type的完全限定名,包括命名空间)

    三、实例3 (设置指定实例 属性 的值)

    [csharp] view plain copy
     
    1. FirstClass fClass = new FirstClass();  
    2. PropertyInfo pInstance = typeof(FirstClass).GetProperty("NewsId");//搜索具有指定名称的公共属性  
    3. pInstance.SetValue(fClass, 11, null);//设置指定实例 属性 的值  
    4.   
    5. Console.WriteLine("新闻ID:"+fClass.NewsId);  
    6. Console.WriteLine("新闻图片:"+fClass.NewsImage);  


    //在4px的库内操作获取打印机

    trv_LabelInvoice.Nodes.Clear();

    string strText = string.Empty;
    FieldInfo[] fields = typeof(EnumPrintName).GetFields();
    foreach (FieldInfo field in fields)
    {
    strText = field.Name;

    object[] arrAttributes = field.GetCustomAttributes(typeof(Attribute), true);
    if (arrAttributes != null)
    {
    EnumAttribute objEnumAttribute = arrAttributes.FirstOrDefault(x => x.GetType().Name.Equals(typeof(EnumAttribute).Name)) as EnumAttribute;
    if (objEnumAttribute != null)
    {
    strText = objEnumAttribute.Description;
    }
    }

    if (trv_LabelInvoice.Nodes.IndexOfKey(field.Name) < 0)
    {
    trv_LabelInvoice.Nodes.Add(field.Name, strText);
    }
    }

  • 相关阅读:
    IE浏览器中js使用中文标识符的bug
    Javascript变量作用域
    利用JS的动态语言特性对数组排序
    Javascript动态方法调用与参数修改的问题
    数组的平衡点
    Javascript中各种trim的实现
    js對象的比較
    返回两个数组中非相同的元素
    Javascript中匿名函数的多种调用方式
    SQL Server PreLogin Handshake Acknowledgement Error [duplicate]
  • 原文地址:https://www.cnblogs.com/chengjun/p/8057219.html
Copyright © 2020-2023  润新知