zoukankan      html  css  js  c++  java
  • Attribute

    ①声明自定义特性,以Attribute 结尾,继承Attribute ,一般声明成私密类
    //可以有构造函数,字段,属性
    /// <summary>
           /// 限制特性使用
           /// </summary>
           [AttributeUsage(AttributeTargets.Method | AttributeTargets.Constructor)]//限制特性使用
           public sealed class MyAttributeAttribute : Attribute
           {
               public string Description { get; set; }
    
               public string VersionNumber { get; set; }
    
               public string Reviewer { get; set; }
    
               /// <summary>
               /// 构造函数只能在贴特性的时候使用;
               /// 使用时可以用位置参数和命名参数来构成,如 [MyAttribute(desc,ver,Reviewer="rev")]
               /// </summary>
               public MyAttributeAttribute(string desc, string ver)
               {
                   Description = desc;
                   VersionNumber = ver;
    
               }
    
           }
    ②限制特性使用:AttributeUsage
     //AttributeUsage有3个重要属性,ValidOn(保存特性能应用到的目标类型的列表。构造函数的第一个参数必须是AttributeTarget类型的枚举值),Inherited(一个布尔值,指示特性是否会被装饰类型的派生类所继承),AllowMutiple(指示目标是否被引用多个特性的实例的布尔值)
       //AttributeUsage 构造函数接收单个位置参数,表示该特性能贴到的目标类型,就是ValidOn;我们可以使用按位或运算来组合使用类型
      [AttributeUsage(
               AttributeTargets.Method | AttributeTargets.Constructor,
               Inherited=false,//可选的,命名参数
               AllowMultiple = false)]//可选的,命名参数
    

    访问特性:得到特性之后,我们就能根据特性的对象获取由构造函数贴进去的属性值 

    ③访问特性IsDefined
      [AttributeUsage(AttributeTargets.Class)]
           public sealed class ReviewCommentAttribute : Attribute
           {
               public string VersionNumber { get; set; }
               public string Reviewer { get; set; }
               public ReviewCommentAttribute(string Rev, string ver)
               {
                   Reviewer = Rev;
                   VersionNumber = ver;
               }
    
    
           }
            [ReviewComment("check it out","2.4")]
            public class TestAttribute
            {
    
            }
     static void Main(string[] args)
            {
                TestAttribute tt = new TestAttribute();
                Type type = tt.GetType();
                bool isDefined= type.IsDefined(typeof(ReviewCommentAttribute), false);
                if (isDefined)
                {
                    Console.WriteLine("ReviewComment is Applied to type {0}");
                    
                }   
    }
    ④访问特性GetCustomAttributes方法
       1.typeof.GetCustomAttribute(typeof(TestAttribute ),false);
       2.//GetCustomerAttribute 方法返回应用到结构特性的数组,因为是object[],所以我们需要强转转为相应的特性类型
     Type type= typeof(TestAttribute);
                object[] AttArr = type.GetCustomAttributes(false);
                foreach (Attribute a in AttArr)
                {
                    ReviewCommentAttribute attr = a as ReviewCommentAttribute;
                    if (null!=attr)
                    {
                        Console.WriteLine("VersionNumber:{0}",attr.VersionNumber);
                        Console.WriteLine("Reviewer:{0}", attr.Reviewer);
                        Console.WriteLine("Description:{0}", attr.Description);
    
                    }
                }
    

      

  • 相关阅读:
    读书笔记 ASP.NET 2.0编程珠玑
    为什么公司招聘一个好员工很难,程序员找份好工作也不容易
    读书笔记 ASP.NET 2.0高级编程 第31章 配置
    Win7 x64 旗舰版下重新注册IIS7.5
    T_SQL 开发的13个Tips
    报表服务扩展:基于WCF技术的报表服务扩展
    实现多国语言的Reporting Services项目
    技术人生:如何成为一位优秀的程序员
    幸福框架:待实现的基础应用列表
    技术人生:做人十心机
  • 原文地址:https://www.cnblogs.com/entclark/p/7900779.html
Copyright © 2011-2022 走看看