属性和反射

Metadata is information about the data that is information about the types ,code, assembly and so forth stored along with the program.Attributes are a mechanism for adding metadata,such as compiler instructions and other data about your data,methods and classes, to the program itself.

An attribute is an object that represents data you want to associate with an element in your program .The element to which you attach an attribute is referred as the target of the attribute.

 

1.属性目标

属性有作用对象的种类,这称为属性目标。在AttributeTargets中声明

2.使用属性

放在方括号里面,并紧贴作用的元素之前,作用于assembly的除外,要放在文件头部(using语句之后)。可以并列排放组合,也可以使用逗号分隔。

3.自定义属性--直接看代码

4.反射

反射主要有4种用途-1.查看元数据  2.进行类型发现  3.对方法和properties的迟绑定  4.运行时创建新类型

4.1查看元数据

Type类是反射类的核心,provided to discover the attribute of a member and provide access to the metadata.Type encapsulate a representation of the type of an object.The Type class the primary way to access metadata.Type encapsulates information about the member of a class(eg. fields,methods,properties,events,etc).

4.2 类型发现

This allows you to examine the types in an assembly and interact with or instantiate those types. You can use reflection to explore and examine the contents of an assembly.You can find the types associated with a module;the methods,fields,properties,and events associated with a type,as well as the signatures of each of the type`s methods;the interfaces supported by the type;and the type`s base class.

查看一个类型的所有成员

仅查找方法

指定查找

4.3 对方法和properties的迟绑定

allows the programmer to invoke properties and methods on objects dynamically instantiated,based on type discovery.This in also known as dynamic invocation.

可能需要使用Activator动态加载一个类的实例,如果对于一个静态的类则不需要。解释一下:

1.首先获得类型,

2.然后获得方法,传入函数原型

3.准备好方法的参数

4.调用,注意第一个参数,如果是静态类型,传入静态类型的type,否则要传入instance的名字。

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值