|
导读通过反射命名空间中的类以及 System.Type,您可以获取有关已加载的程序集和在其中定义的类型(如类、接口和值类型)的信息。您也可以使用反射在运行时创建类型实例,然后调用和访问这些实例。反射概述... 通过反射命名空间中的类以及 System.Type,您可以获取有关已加载的程序集和在其中定义的类型(如类、接口和值类型)的信息。您也可以使用反射在运行时创建类型实例,然后调用和访问这些实例。反射概述公共语言运行库加载器管理应用程序域。这种管理包括将每个程序集加载到相应的应用程序域以及控制每个程序集中类型层次结构的内存布局。 程序集包含模块,而模块包含类型,类型又包含成员。反射则提供了封装程序集、模块和类型的对象。您可以使用反射动态地创建类型的实例,将类型绑定到现有对象,或从现有对象中获取类型。然后,可以调用类型的方法或访问其字段和属性。反射通常具有以下用途:
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(); } } |
温馨提示:喜欢本站的话,请收藏一下本站!