时间: 2021-07-31 作者:daque
经过曲射定名空间中的类以及 system.type,您不妨获得相关已加载的步调集和在个中设置的典型(如类、接口和值典型)的消息。您也不妨运用曲射在运转时创造典型范例,而后挪用和考察那些范例。曲射概括
大众谈话运转库加载器管理当用步调域。这种处置囊括将每个步调集加载到相映的运用步调域以及遏制每个步调会合典型档次构造的外存构造。步调集包括模块,而模块包括典型,典型又包括分子。曲射则供给了封装步调集、模块和典型的东西。您不妨运用曲射动静地创造典型的范例,将典型绑定到现有东西,或从现有东西中获得典型。而后,不妨挪用典型的本领或考察其字段和属性。曲射常常具备以次用处:
运用 assembly 设置和加载步调集,加载在步调集清单中列出的模块,以及此后步调会合搜索典型并创造该典型的范例。
运用 module 领会如次的一致消息:包括模块的步调集以及模块中的类等。您还不妨获得在模块上设置的一切全部本领或其余一定的非全部本领。
运用 constructorinfo 领会如次的一致消息:结构因变量的称呼、参数、考察化装符(如 public 或 private)和实行精细消息(如 abstract 或 virtual)等。运用 type 东西的 getconstructors 或 getconstructor 本领来挪用一定的结构因变量。
运用 methodinfo 来领会如次的一致消息:本领的称呼、归来典型、参数、考察化装符(如 public 或 private)和实行精细消息(如 abstract 或 virtual)等。运用 type 东西的 getmethods 或 getmethod 本领来挪用一定的本领。
运用 fieldinfo 来领会如次的一致消息:字段的称呼、考察化装符(如 public 或 private)和实行精细消息(如 static)等;并获得或树立字段值。
运用 eventinfo 来领会如次的一致消息:事变的称呼、事变处置步调数据典型、自设置属性、证明典型和曲射典型等;并增添或移除事变处置步调。
运用 propertyinfo 来领会如次的一致消息:属性的称呼、数据典型、证明典型、曲射典型和只读或可写状况等;并获得或树立属性值。
运用 parameterinfo 来领会如次的一致消息:参数的称呼、数据典型、参数是输出参数仍旧输入参数,以及参数在本领出面中的场所等。
system.reflection.emit 定名空间的类供给了一种特出情势的曲射,使您不妨在运转时结构典型。曲射也可用来创造称作典型欣赏器的运用步调,它运用户不妨采用典型,而后察看相关选定典型的消息。曲射再有其余少许用处。jscript 等谈话编写翻译器运用曲射来结构标记表。system.runtime.serialization 定名空间中的类运用曲射来考察数据并决定要长久生存的字段。system.runtime.remoting 定名空间中的类经过序列化来转弯抹角地运用曲射。察看典型消息
system.type 类对于曲射起着中心的效率。当曲射乞求加载的典型时,大众谈话运转库将为它创造一个 type 东西。您不妨运用 type 东西的本领、字段、属性和嵌套类来搜索相关该典型的一切消息。在运用 assembly.gettype 或 assembly.gettypes 时传入所需典型的称呼,不妨从尚未加载的步调会合获得 type 东西。运用 type.gettype 可从已加载的步调会合获得 type 东西。运用 module.gettype 和 module.gettypes 可获得模块 type 东西。以次代码示例表露在获得步调集的 assembly 东西和模块时所必定的语法。[c#]
// get the mscorlib assembly in which the object is defined.
assembly a = typeof(object).module.assembly;
以次示例代码证明怎样从已加载的步调会合获得 type 东西。[c#]
// load an assembly using its file name.
assembly a = assembly.loadfrom ("myexe.exe");
// get the type names from the assembly.
type [] types2 = a.gettypes ();
foreach (type t in types2)
{
console.writeline (t.fullname);
}
获得 type 东西之后,不妨经过多种本领来领会相关该典型分子的消息。比方,经过挪用 type.getmembers 本领(该本领将获得对暂时典型的每个分子举行刻画的一组 memberinfo 东西),您不妨获得相关该典型的一切分子的消息。您也不妨在 type 类上运用本领,以检索相关按称呼指定的一个或多个结构因变量、本领、事变、字段或属性的消息。比方,type.getconstructor 封装暂时类的一定结构因变量。即使具备 type 东西,则不妨运用 type.module 属性来获得一个封装该典型地方模块的东西。运用 module.assembly 属性可搜索封装模块地方步调集的东西。运用 type.assembly 属性可径直获得封装典型的步调集。system.type 和 constructorinfo
以次代码示例表露怎样列出一个类(此示例中为 string 类)的结构因变量。[c#]
// this program lists all the public constructors
// of the system.string class.
using system;
using system.reflection;
class listmembers {
public static void main(string[] args) {
type t = typeof(system.string);
console.writeline ("listing all the public constructors of the {0} type", t);
// constructors
constructorinfo[] ci = t.getconstructors(bindingflags.public | bindingflags.instance);
console.writeline ("//constructors");
printmembers (ci);
}
public static void printmembers(memberinfo [] ms) {
foreach (memberinfo m in ms) {
console.writeline ("{0}{1}", " ", m);
}
console.writeline();
}
}
memberinfo、methodinfo、fieldinfo 和 propertyinfo
运用 memberinfo、methodinfo、fieldinfo 或 propertyinfo 东西可获得相关典型的本领、属性、事变、字段的消息。以次代码示例运用 memberinfo 来列出 system.io.file 类中的分子数目并运用 system.type.ispublic 属性来决定该类的看来性。[c#]
using system;
using system.io;
using system.reflection;
class mymemberinfo
{
public static void main(string[] args)
{
console.writeline ("\nreflection.memberinfo");
// get the type and memberinfo.
type mytype =type.gettype("system.io.file");
memberinfo[] mymemberinfoarray = mytype.getmembers();
// get and display the declaringtype method.
console.writeline("\nthere are {0} members in {1}.",
mymemberinfoarray.length, mytype.fullname);
console.writeline("{0}.", mytype.fullname);
if (mytype.ispublic)
{
console.writeline("{0} is public.", mytype.fullname);
}
}
}
以次代码示例观察指定分子的典型。它对 memberinfo 类的一个分子实行曲射,而后列出其典型。[c#]
// this code displays information about the getvalue method of fieldinfo.
using system;
using system.reflection;
class mymethodinfo {
public static int main() {
console.writeline("reflection.methodinfo");
// get and display the type.
type mytype = type.gettype("system.reflection.fieldinfo");
// specify the member for which you want type information here.
methodinfo mymethodinfo = mytype.getmethod("getvalue");
console.writeline(mytype.fullname + "." + mymethodinfo.name);
// get and display the membertype property.
membertypes mymembertypes = mymethodinfo.membertype;
if (membertypes.constructor == mymembertypes) {
console.writeline("membertype is of type all");
}
else if (membertypes.custom == mymembertypes) {
console.writeline("membertype is of type custom");
}
else if (membertypes.event == mymembertypes) {
console.writeline("membertype is of type event");
}
else if (membertypes.field == mymembertypes) {
console.writeline("membertype is of type field");
}
else if (membertypes.method == mymembertypes) {
console.writeline("membertype is of type method");
}
else if (membertypes.property == mymembertypes) {
console.writeline("membertype is of type property");
}
else if (membertypes.typeinfo == mymembertypes) {
console.writeline("membertype is of type typeinfo");
}
return 0;
}
}
以次示例代码运用一切的曲射 *info 类以及 bindingflags 来列出指定类的一切分子(结构因变量、字段、属性、事变和本领),并将那些分子分别为静态和范例类型。[c#]
// this program lists all the members of the
// system.io.bufferedstream class.
using system;
using system.io;
using system.reflection;
class listmembers {
public static void main(string[] args) {
// specify the class here.
type t = typeof (system.io.bufferedstream);
console.writeline ("listing all the members (public and non public) of the {0} type", t);
// static fields are listed first.
fieldinfo [] fi = t.getfields (bindingflags.static |
bindingflags.nonpublic | bindingflags.public);
console.writeline ("// static fields");
printmembers (fi);
// static properties
propertyinfo [] pi = t.getproperties (bindingflags.static |
bindingflags.nonpublic | bindingflags.public);
console.writeline ("// static properties");
printmembers (pi);
// static events
eventinfo [] ei = t.getevents (bindingflags.static |
bindingflags.nonpublic | bindingflags.public);
console.writeline ("// static events");
printmembers (ei);
// static methods
methodinfo [] mi = t.getmethods (bindingflags.static |
bindingflags.nonpublic | bindingflags.public);
console.writeline ("// static methods");
printmembers (mi);
// constructors
constructorinfo [] ci = t.getconstructors (bindingflags.instance |
bindingflags.nonpublic | bindingflags.public);
console.writeline ("// constructors");
printmembers (ci);
// instance fields
fi = t.getfields (bindingflags.instance | bindingflags.nonpublic |
bindingflags.public);
console.writeline ("// instance fields");
printmembers (fi);
// instance properites
pi = t.getproperties (bindingflags.instance | bindingflags.nonpublic |
bindingflags.public);
console.writeline ("// instance properties");
printmembers (pi);
// instance events
ei = t.getevents (bindingflags.instance | bindingflags.nonpublic |
bindingflags.public);
console.writeline ("// instance events");
printmembers (ei);
// instance methods
mi = t.getmethods (bindingflags.instance | bindingflags.nonpublic |
bindingflags.public);
console.writeline ("// instance methods");
printmembers (mi);
console.writeline ("\r\npress return to exit.");
console.read();
}
public static void printmembers (memberinfo [] ms) {
foreach (memberinfo m in ms) {
console.writeline ("{0}{1}", " ", m);
}
console.writeline();
}
}