zoukankan      html  css  js  c++  java
  • CLR Via CSharp读书笔记(18):定制Attributes

    检测定制的attribute:

    定制attribute之后,主要在于检测。主要的方法有System.TypeIsDefined方法,System.Attribute类定义的三个静态方法用以获取与一个目标关联的attribute: IsDefined, GetCustomAttributes(用于将AllowMultiple设为true的attribute,或者列出所有已应用的attribute)和GetCustomAttribute(通常用于将AlloMultiple设为false的attribute).

    检测时不创建从Attribute派生的对象

    使用System.Reflection.CustomAttributeData类的GetCustomAttributes方法,有四个重载版本,分别接受Assembly, Module, ParameterInfoMemberInfo. 通常先用Assembly的静态方法ReflectionOnlyLoad加载一个程序集,再用CustomAttributeData类分析这个程序集的元素据中的attribute。

    条件attribute类:

    // #define TEST
    #define VERIFY
    
    using System;
    using System.Diagnostics;
    
    [Conditional("TEST")]
    [Conditional("VERIFY")]
    public sealed class CondAttribute : Attribute {
    }
    
    [Cond]
    public static class Program{  
       public static void Main() {
          Console.WriteLine("CondAttribute is {0}applied to Program type.",
             Attribute.IsDefined(typeof(Program), typeof(CondAttribute)) ? "" : "not ");
       }
    }
  • 相关阅读:
    第二章初识MySQL
    第一章 数据库
    Java&SQL7
    Java&SQL
    Java&SQL6
    Java&SQL5
    Java&SQL4
    Java&SQL3
    Java&SQL2
    博客地址已搬迁
  • 原文地址:https://www.cnblogs.com/thlzhf/p/2805480.html
Copyright © 2011-2022 走看看