reflection
对象用于在运行时获取类型信息。可以访问正在运行的程序的元数据的类在system.reflection
命名空间中。
system.reflection
命名空间包含允许获取有关应用程序的信息的类,并向应用程序动态添加类型,值和对象。
反射有以下应用:
我们在上一章中提到使用反射可以查看属性信息。
需要初始化system.reflection
类的memberinfo
对象,以发现与类相关联的属性。可将目标类的对象定义为:
system.reflection.memberinfo info = typeof(myclass);
以下程序演示如下:
using system;
[attributeusage(attributetargets.all)]
public class helpattribute : system.attribute
{
public readonly string url;
public string topic // topic is a named parameter
{
get
{
return topic;
}
set
{
topic = value;
}
}
public helpattribute(string url) // url is a positional parameter
{
this.url = url;
}
private string topic;
}
[helpattribute("information on the class myclass")]
class myclass
{
}
namespace attributeappl
{
class program
{
static void main(string[] args)
{
system.reflection.memberinfo info = typeof(myclass);
object[] attributes = info.getcustomattributes(true);
for (int i = 0; i < attributes.length; i++)
{
system.console.writeline(attributes[i]);
}
console.readkey();
}
}
}
当它被编译并运行时,它显示附加到类myclass
的自定义属性的名称:
helpattribute
在这个例子中,我们使用上一章创建的debuginfo
属性,并使用反射来读取rectangle
类中的元数据。
using system;
using system.reflection;
namespace bugfixapplication
{
//a custom attribute bugfix to be
//assigned to a class and its members
[attributeusage(attributetargets.class |
attributetargets.constructor |
attributetargets.field |
attributetargets.method |
attributetargets.property,
allowmultiple = true)]
public class debuginfo : system.attribute
{
private int bugno;
private string developer;
private string lastreview;
public string message;
public debuginfo(int bg, string dev, string d)
{
this.bugno = bg;
this.developer = dev;
this.lastreview = d;
}
public int bugno
{
get
{
return bugno;
}
}
public string developer
{
get
{
return developer;
}
}
public string lastreview
{
get
{
return lastreview;
}
}
public string message
{
get
{
return message;
}
set
{
message = value;
}
}
}
[debuginfo(45, "sukida", "12/8/2017", message = "return type mismatch")]
[debuginfo(49, "maxsu", "10/10/2017", message = "unused variable")]
class rectangle
{
//member variables
protected double length;
protected double width;
public rectangle(double l, double w)
{
length = l;
width = w;
}
[debuginfo(55, "sukida", "19/10/2017", message = "return type mismatch")]
public double getarea()
{
return length * width;
}
[debuginfo(56, "sukida", "19/10/2017")]
public void display()
{
console.writeline("length: {0}", length);
console.writeline("width: {0}", width);
console.writeline("area: {0}", getarea());
}
}//end class rectangle
class executerectangle
{
static void main(string[] args)
{
rectangle r = new rectangle(14.5, 17.5);
r.display();
type type = typeof(rectangle);
//iterating through the attribtues of the rectangle class
foreach (object attributes in type.getcustomattributes(false))
{
debuginfo dbi = (debuginfo)attributes;
if (null != dbi)
{
console.writeline("bug no: {0}", dbi.bugno);
console.writeline("developer: {0}", dbi.developer);
console.writeline("last reviewed: {0}", dbi.lastreview);
console.writeline("remarks: {0}", dbi.message);
}
}
//iterating through the method attribtues
foreach (methodinfo m in type.getmethods())
{
foreach (attribute a in m.getcustomattributes(true))
{
debuginfo dbi = (debuginfo)a;
if (null != dbi)
{
console.writeline("bug no: {0}, for method: {1}", dbi.bugno, m.name);
console.writeline("developer: {0}", dbi.developer);
console.writeline("last reviewed: {0}", dbi.lastreview);
console.writeline("remarks: {0}", dbi.message);
}
}
}
console.readline();
}
}
}
当上述代码被编译并执行时,它产生以下结果:
length: 14.5
width: 17.5
area: 253.75
bug no: 45
developer: sukida
last reviewed: 12/8/2017
remarks: return type mismatch
bug no: 49
developer: maxsu
last reviewed: 10/10/2017
remarks: unused variable
bug no: 55, for method: getarea
developer: sukida
last reviewed: 19/10/2017
remarks: return type mismatch
bug no: 56, for method: display
developer: sukida
last reviewed: 19/10/2017
remarks: