Reflection,中文翻译为反射。这是.Net中获取运行时类型信息的方式,.Net的应用程序由几个部分:‘程序集(Assembly)’、‘模块(Module)’、‘类型(class)’组成,而反射提供一种编程的方式,让程序员可以在程序运行期获得这几个组成部分的相关信息,例如:Assembly类可以获得正在运行的装配件信息,也可以动态的加载装配件,以及在装配件中查找类型信息,并创建该类型的实例。Type类可以获得对象的类型信息,此信息包含对象的所有要素:方法、构造器、属性等等,通过Type类可以得到这些要素的信息,并且调用之。MethodInfo包含方法的信息,通过这个类可以得到方法的名称、参数、返回值等,并且可以调用之。诸如此类,还有FieldInfo、EventInfo等等,这些类都包含在System.Reflection命名空间下。
一、Type类于获取类型信息
System.Type 类对于反射起着核心的作用。当反射请求加载的类型时,公共语言运行库将为它创建一个 Type。您可以使用 Type 对象的方法、字段、属性和嵌套类来查找有关该类型的所有信息。
大家运行一下下面的代码根据结果分析一下就能比较清楚的理解Type了
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->namespaceConsoleApplication2
{
classProgram
{
staticvoidMain(string[]args)
{
MyClassm=newMyClass();
Typetype=m.GetType();
Console.WriteLine("类型名:"+type.Name);
Console.WriteLine("类全名:"+type.FullName);
Console.WriteLine("命名空间名:"+type.Namespace);
Console.WriteLine("程序集名:"+type.Assembly);
Console.WriteLine("模块名:"+type.Module);
Console.WriteLine("基类名:"+type.BaseType);
Console.WriteLine("是否类:"+type.IsClass);
Console.WriteLine("类的公共成员:");
MemberInfo[]memberInfos=type.GetMembers();//得到所有公共成员
foreach(variteminmemberInfos)
{
Console.WriteLine("{0}:{1}",item.MemberType,item);
}
}
}
classMyClass
{
publicstringm;
publicvoidtest()
{}
publicintMyProperty{get;set;}
}
}
二、获取程序集元数据
Assembly类定义了一个程序集,它是一个可重用、无版本冲突并且可自我描述的公共语言运行库应用程序构造块。因为程序集中是使用元数据进行自我描述的,所以我们就能通过其元数据得到程序集内部的构成。结合Assembly和反射能够获取程序集的元数据,但是首先要将程序集装入内存中。可以使用Assembly类的多种静态Load方法加载程序集。
下面的程序显示程序集的信息
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->publicstaticvoidMain()
{
//获取当前执行代码的程序集
Assemblyassem=Assembly.GetExecutingAssembly();
Console.WriteLine("程序集全名:"+assem.FullName);
Console.WriteLine("程序集的版本:"+assem.GetName().Version);
Console.WriteLine("程序集初始位置:"+assem.CodeBase);
Console.WriteLine("程序集位置:"+assem.Location);
Console.WriteLine("程序集入口:"+assem.EntryPoint);
Type[]types=assem.GetTypes();
Console.WriteLine("程序集下包含的类型:");
foreach(varitemintypes)
{
Console.WriteLine("类:"+item.Name);
}
}
三、动态加载类型
早绑定是在编译时绑定对象类型,而晚绑定是在运行时才绑定对象的类型。利用反射可以实现晚绑定,即动态加载类型,并调用他们的方法,下边是MSDN中的一个例子,详细的解释信息见注释
<!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />-->namespaceConsoleApplication2
{
publicclassExample
{
privateintfactor;
publicExample(intf)
{
factor=f;
}
publicintSampleMethod(intx)
{
Console.WriteLine("/nExample.SampleMethod({0})executes.",x);
returnx*factor;
}
publicstaticvoidMain()
{
//获取当前执行代码的程序集
Assemblyassem=Assembly.GetExecutingAssembly();
Console.WriteLine("AssemblyFullName:");
Console.WriteLine(assem.FullName);
//TheAssemblyNametypecanbeusedtoparsethefullname.
AssemblyNameassemName=assem.GetName();
Console.WriteLine("/nName:{0}",assemName.Name);
Console.WriteLine("Version:{0}.{1}",
assemName.Version.Major,assemName.Version.Minor);
Console.WriteLine("/nAssemblyCodeBase:");
Console.WriteLine(assem.CodeBase);
//从程序集众创建一个Example实例并且用object类型的引用o指向它,同时调用一个输入参数的构造函数
Objecto=assem.CreateInstance("ConsoleApplication2.Example",false,
BindingFlags.ExactBinding,
null,newObject[]{2},null,null);
//构造Example类的一个晚绑定的方法SampleMethod
MethodInfom=assem.GetType("ConsoleApplication2.Example").GetMethod("SampleMethod");
//调用刚才实例化好的Example对象o中的SampleMethod方法,传入的参数为42
Objectret=m.Invoke(o,newObject[]{42});
Console.WriteLine("SampleMethodreturned{0}.",ret);
Console.WriteLine("/nAssemblyentrypoint:");
Console.WriteLine(assem.EntryPoint);
}
}