当前位置: 移动技术网 > IT编程>开发语言>.net > 详解C#特性和反射(一)

详解C#特性和反射(一)

2018年09月21日  | 移动技术网IT编程  | 我要评论

封魔末世录,h的动漫,中国电线电缆网

  使用特性(attribute)可以将描述程序集的信息和描述程序集中任何类型和成员的信息添加到程序集的元数据和il代码中,程序可以在运行时通过反射获取到这些信息;

 

  一、通过直接或间接的继承自抽象类system.attribute可以创建自定义的特性类,自定义的特性类必须声明为公共类,命名一般使用attribute结尾以保证可读性,自定义特性可以附加到大多数元素声明中,也可以使用特性system.attributeusage(该特性只能用于特性类声明)指定该特性所能生效的范围及其它特性特征参数:

[attributeusage(attributetargets.all, inherited = true, allowmultiple = true)]
//其中,枚举组合attributetargets指定该特性生效的范围,默认为all即所有范围;
//布尔值inherited指定应用该特性的成员在派生类中是否会继承该特性,默认为true;
//布尔值allowmultiple指定能否为同一个元素指定多个此特性,默认为false public class myselfattribute : attribute { public string classname { get; private set; } public string author; public myselfattribute(string classname) { this.classname = classname; } }

  其中特性类的构造函数中的参数称为位置参数(positional parameters),类中的其他公共字段和属性称为命名参数(named parameter), 通常情况下,将所有必选的参数定义为位置参数,将所有可选的参数定义为命名参数;特性类和普通类一样可以进行构造函数的重载以适应各种情况下初始化参数的组合使用;

 

  二、使用特性时,通过方括号[]将特性名称括起来,并置于使用该特性的元素声明的上方或前方以指定特性,使用时可以省略attribute后缀,根据想要初始化时调用特性类构造函数的不同,需要将该构造函数所需的参数(即位置参数)的值按照顺序传入,还可以选择是否指定命名参数的值,命名参数的值通过赋值运算符=显式指定:

[myself("myclass", author = "me")]
//这个声明在概念上等效于: //myselfattribute myselfobj = new myselfattribute("myclass"); //myselfobj.author = "me"; //[myself("myclass", author = "you")] //特性myself可以对同一元素指定多次 //也可以将多个特性合并在一个方括号里: //[myself("myclass", author = "me"), myself("myclass", author = "you")] public class myclass { public void myfunc([myself("myparameter")]int mynum) //在方法参数列表中给参数指定特性 { //do… } }

   经过编译后,在元数据中查看到的类型定义中的特性信息:

typedef #1 (02000002)
-------------------------------------------------------
    typdefname: myclass  (02000002)
    flags     : [public] [autolayout] [class] [ansiclass] [beforefieldinit]  (00100001)
    extends   : 01000013 [typeref] system.object
    method #1 (06000001) 
    -------------------------------------------------------
        methodname: myfunc (06000001)
        flags     : [public] [virtual] [hidebysig] [newslot]  (000001c6)
        rva       : 0x00002050
        implflags : [il] [managed]  (00000000)
        callcnvntn: [default]
        hasthis 
        returntype: void
        1 arguments
            argument #1:  i4
        1 parameters
            (1) paramtoken : (08000001) name : mynum flags: [none] (00000000)
            customattribute #1 (0c000003)
            -------------------------------------------------------
                customattribute type: 06000009
                customattributename: myselfattribute :: instance void .ctor(class system.string)
                length: 16
                value : 01 00 0b 4d 79 50 61 72  61 6d 65 74 65 72 00 00 >   myparameter  <
                ctor args: ("myparameter")


    method #2 (06000002) 
    -------------------------------------------------------
        methodname: .ctor (06000002)
        flags     : [public] [hidebysig] [reuseslot] [specialname] [rtspecialname] [.ctor]  (00001886)
        rva       : 0x0000205a
        implflags : [il] [managed]  (00000000)
        callcnvntn: [default]
        hasthis 
        returntype: void
        no arguments.

    customattribute #1 (0c000015)
    -------------------------------------------------------
        customattribute type: 06000009
        customattributename: myselfattribute :: instance void .ctor(class system.string)
        length: 24
        value : 01 00 07 4d 79 43 6c 61  73 73 01 00 54 0e 06 41 >   myclass  t  a<
                      : 75 74 68 6f 72 02 4d 65                          >uthor me        <
        ctor args: ("myclass")

  在il代码中查看到的类型定义中的特性信息:

 

 

  三、系统预定义的一些常用特性:

 

   四、特性通常配合反射起作用,在指定的时机通过反射获得自定义特性并对其进行操作,具体内容在下一章中介绍;

 


如果您觉得阅读本文对您有帮助,请点一下“推荐”按钮,您的认可是我写作的最大动力!

作者:minotauros
出处:

本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 

如对本文有疑问,请在下面进行留言讨论,广大热心网友会与你互动!! 点击进行留言回复

相关文章:

验证码:
移动技术网