zoukankan      html  css  js  c++  java
  • 反射手册笔记 5.元数据

    1.Attribute,属性(特性),又名元数据,自定义属性。编译期会解释属性,并将对应的标志插入到IL中。
      AOP:面向方面的编程,以声明性的方式控制属性。
      .NET已有的属性类:
        1)Serializable: 仅可用于Class/Struct/Enum/Delegate
        2)NotSerialized:仅用于字段
        3)AssemblyVersion:仅用于程序集,为其定义版本号
        4)Conditional:仅用于方法,用来确定是否忽略方法
        5)WebService:用于class
        6)WebMethod:用于方法
       
      测试标准属性的数据类型:
        System.Reflection.TypeAttribute枚举,用来确定是否为标准类型:Abstract/Class/Interface/Serializable等等共29个。
        枚举不能遍历,要使用逻辑与 & 进行操作:
       
                TypeAttributes attributes = objHuman.GetType().Attributes;
                
    if ((attribute & TypeAttributes.Serializable) != 0)
                {
                    
    //atttributes枚举为 Public|Serializable|BeforeFieldInit, 
                    
    //与TypeAttributes.Serializable做逻辑与,得到非0值(如果为0说明atttributes中不含Serializable)——是可序列化类型
                }


    2.Attribute类语法:
        Attribute类继承于System.Attribute
        .NET中所有属性类均以Attribute结束(定义部分);使用属性时,可以省略结尾的Attribute部分。
        属性类不可以被继承,要声明为sealed。
        属性类必须以<AttributeUsage>开始,从而指定属性可以用于类/方法/结构/...,如:
           <AttributeUsage(AttributeTarget.Class | AttributeTarget.Struct)——表示该属性类可以用于类和结构。
           AttributeTarget.All表示所有类型。
        通过指定AllowMultiple:=false/true来说明属性是否可以在相同的代码元素中多次使用,如:
        //定义属性类
        [AttributeUsage(AttributeTargets.All, Inherited = false, AllowMultiple = true)]
        
    public class AuthorAttribute : Attribute
        { 
        
        }

        
    //使用属性类
        [Author("Jax.Bao"), Author("Fish.Xu")]
        
    public class CSharpBook
        { 
        
        }

        将参数传递到属性,有两种参数:
           位置参数:强制性要有,按顺序排列参数。通过传递到属性的构造函数,从而完成类的初始化。
           命令参数:可选参数,在位置参数后面,无顺序,参数名与Attribute类中的属性相对应,使用=的语法,如:
              [DllImport("User32.dll", CharSet=CharSet.Unicode)]
          
           技巧:可以为属性类定义多个构造函数,从而具有更大灵活性。

    3.[Conditional("DEBUG")]:"有条件"方法编译
        在方法前A加上[Conditional("DEBUG")]这个标记,并在Main()中调用A方法。
        在Debug状态下,不会执行A方法;但是方法A会编译到IL中,并在相应A方法的IL中标记为Debug,在Main的IL中不会有调用A的动作。
        在Release状态下,方法A同样会编译到IL中,在相应A方法的IL中也会标记为Debug,于是,在Main的IL中就会有调用A的动作。

        * 使用#if DEBUG...#end if也能达到同样效果,据说比[Conditional("DEBUG")]性能高很多。

    4.Assembly属性,全局变量,配合Attribute.GetCustomerAttribute()方法使用,示例如下,
        获取AssemblyInfo.cs中的[assembly: AssemblyTitle("TestStatic")]:
                Attribute attribute = Attribute.GetCustomAttribute(Assembly.GetCallingAssembly(),
                                       
    typeof(AssemblyTitleAttribute), false);

                AssemblyTitleAttribute ta 
    = (AssemblyTitleAttribute)attribute;
                Response.WriteLine(ta.Title);
        这里,false表示对不包括超类中的自定义属性.

    5.定义新的自定义属性:
        MemberInfo有一个GetCustomAttributes()方法,返回一个自定义属性的数组,同样需要一个bool参数,决定是否对包括超类中的自定义属性,示例如下:
                MemberInfo info;
                Object[] attribs 
    = info.GetCustomAttributes(false);
                
    foreach (object attrib in attribs)
                {
                    
    //遍历attribs自定义属性数组
                }

        本章最后一部分讲的是Remoting技术,从略。   
  • 相关阅读:
    Atitit 华为基本法 attilax读后感
    Atitit 华为管理者内训书系 以奋斗者为本 华为公司人力资源管理纲要 attilax读后感
    Atitit 项目版本管理gitflow 与 Forking的对比与使用
    Atitit 管理的模式扁平化管理 金字塔 直线型管理 垂直管理 水平管理 矩阵式管理 网状式样管理 多头管理 双头管理
    Atitit 乌合之众读后感attilax总结 与读后感结构规范总结
    深入理解 JavaScript 异步系列(4)—— Generator
    深入理解 JavaScript 异步系列(3)—— ES6 中的 Promise
    深入理解 JavaScript 异步系列(2)—— jquery的解决方案
    深入理解 JavaScript 异步系列(1)——基础
    使用 github + jekyll 搭建个人博客
  • 原文地址:https://www.cnblogs.com/Jax/p/877045.html
Copyright © 2011-2022 走看看