在.net中,反射是一个运行库类型发现的过程。使用反射服务,可以得到一个给定*.dll或*.exe程序集所包含的所有类型的列表。
命名空间System.Reflection包含了大量与反射相关的类型,要理解如何使用System.Reflection命名空间编程读取.net元数据,首先要理解System.Type类型
System.Type类型定义了很多成员,可以用来检测某个类型的元数据,它们返回的类型大多数位于System.reflection命名空间中。如:Type.GetMethods()返回一个MethodInfo类型的数组。Type类型的各种成员就不一一列举。
下面是得到Type类型的三种方式:
1.System.Object.GetType()
object obj=new Object();
Type t=obj.GetType();
2.typeOf()
Type t=typeof(Object);
3.System.Type.GetType()
该方法的传入类型的完全限定名
Type t=System.Type.GetType(“System.Object”);
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace TypeReflection { class Program { static void Main(string[] args) { //TypeTest(); //AsmTest(); //string displayname = null; //displayname = "System.Windows.Forms," + "Version=4.0.0.0," + "PublicKeyToken=b77a5c561934e089," + @"Culture="""; //Assembly asm = Assembly.Load(displayname); //DisplayTypesinAsm(asm); ActivatorTest(); } private static void TypeTest() { do { Console.WriteLine("Enter a type name or enter Q to quit:"); string str = Console.ReadLine(); if (str.ToUpper() == "Q") { break; } try { Type t = Type.GetType(str); GetFields(t); GetProperties(t); GetMethods(t); } catch { Console.WriteLine("Sorry,can't find type."); } } while (true); }
//获取字段 static void GetFields(Type t) { if (t != null) { Console.WriteLine("*******Fields********"); FieldInfo[] fields = t.GetFields(); foreach(var field in fields) { Console.WriteLine("{0} {1}", field.FieldType, field.Name); } } }
//获取属性 static void GetProperties(Type t) { if (t != null) { Console.WriteLine("*******Properties********"); PropertyInfo[] properties = t.GetProperties(); foreach (var property in properties) { Console.WriteLine("{0} {1}", property.PropertyType, property.Name); } } }
//获取方法 static void GetMethods(Type t) { if (t != null) { Console.WriteLine("*******Methods********"); MethodInfo[] methods = t.GetMethods(); foreach (var method in methods) { string str = method.ReturnType.ToString() + " " + method.Name + " ("; foreach (var p in method.GetParameters()) { str += p.ParameterType + " " + p.Name + ","; } str += ");"; Console.WriteLine(str); } } } static void AsmTest() { do { Console.WriteLine("Enter a Assembly name or enter Q to quit:"); string str = Console.ReadLine(); if (str.ToUpper() == "Q") { break; } try { Assembly asm = Assembly.LoadFrom(str);//加载程序集 DisplayTypesinAsm(asm); } catch { Console.WriteLine("Sorry,can't find assembly."); } } while (true); } static void DisplayTypesinAsm(Assembly asm) { if (asm != null) { Console.WriteLine("********Types in Assembly********"); Console.WriteLine("=>{0}", asm.FullName); var types = from type in asm.GetTypes() where type.IsEnum && type.IsPublic select type; foreach (var t in types) { Console.WriteLine("Type:{0}", t); } } } //晚期绑定 static void ActivatorTest() { try { string displayname = null; displayname = "System.Windows.Forms," + "Version=4.0.0.0," + "PublicKeyToken=b77a5c561934e089," + @"Culture="""; Assembly asm = Assembly.Load(displayname);//加载程序集 Type t = asm.GetType("System.Windows.Forms.Form");//获取程序集中的类型 object obj= System.Activator.CreateInstance(t);//获取类型的实例 MethodInfo mi = t.GetMethod("ToString");//获取类型的实例方法 object s= mi.Invoke(obj,null);//方法的调用 Console.WriteLine(s); } catch(Exception ex) { Console.WriteLine(ex.Message); } } } }