相关链接:
http://www.cnblogs.com/yaozhenfa/p/CSharp_Reflection_1.html
http://blog.csdn.net/educast/article/details/2894892
成员:字段、属性、方法等所有在类里面的东西
0.public enum BindingFlags
搜索条件。常取的值为:
Instance:指定实例成员将包括在搜索中。
Static:指定静态成员将包括在搜索中。
NonPublic:指定非公共成员将包括在搜索中。
Public:指定公共成员将包括在搜索中。
DeclaredOnly:不考虑继承成员。
1.public enum MemberTypes
成员类型。常取的值为:
Constructor:指定该成员是一个构造函数。
Field:指定该成员是一个字段。
Method:指定该成员是一个方法。
Property:指定该成员是一个属性。
2.public abstract class MemberInfo
成员信息。常用属性:
MemberType:指示此成员的类型。
Name:获取当前成员的名称。
3.public abstract Type FieldType
字段类型。例如System.Int32
4.public abstract class FieldInfo
字段信息。
常用属性:
FieldType:获取此字段对象的类型。
Name:获取当前成员的名称。
常用方法:
public abstract object GetValue(object obj):返回给定对象的字段的值。
public void SetValue(object obj,object value):设置给定对象的字段值。
5.public abstract class PropertyInfo
属性信息。
常用方法:
public MethodInfo GetGetMethod():返回此属性的公共 get 访问器。
public MethodInfo GetSetMethod():返回此属性的公共 set 访问器。
6.public class ParameterInfo
参数信息。
常用属性:
ParameterType:获取该参数的 Type。
7.public abstract class MethodInfo
方法信息。
常用属性:
ReturnType:获取此方法的返回类型。
常用方法:
public abstract ParameterInfo[] GetParameters():获取指定的方法或构造函数的参数。
public object Invoke(object obj,object[] parameters):使用指定的参数调用当前实例所表示的方法或构造函数。
反射用到的主要类:Type & Assembly
8.public abstract class Type
表示类型声明:类类型、接口类型、数组类型、值类型、枚举类型、类型参数、泛型类型定义,以及开放或封闭构造的泛型类型。
获取给定类型的Type引用有3种常用方式:
a.使用 typeof 运算符:Type t = typeof(string);
b.使用对象.GetType()方法:string s = "grayworm";Type t = s.GetType();
c.使用Type类的静态方法GetType():Type t = Type.GetType("System.String");
常用属性:
Name:数据类型名
FullName:数据类型的完全限定名(包括命名空间名)
Namespace:定义数据类型的命名空间名
IsAbstract:指示该类型是否是抽象类型
IsArray:指示该类型是否是数组
IsClass:指示该类型是否是类
IsEnum:指示该类型是否是枚举
IsInterface:指示该类型是否是接口
IsPublic:指示该类型是否是公有的
IsSealed:指示该类型是否是密封类
IsValueType:指示该类型是否是值类型
常用方法:
public MemberInfo[] GetMembers():返回为当前 Type 的所有公共成员。
public FieldInfo[] GetFields():返回当前 Type 的所有公共字段。
public PropertyInfo[] GetProperties():返回为当前 Type 的所有公共属性。
public MethodInfo[] GetMethods():返回为当前 Type 的所有公共方法。
以上方法都可以通过设置BindingFlags进行条件搜索,使其不局限与"公共"。
注意使用GetMethods时,因为属性本质上也算是方法,所以要判断参数的数量与类型
实例:
//查看类中的构造方法: NewClassw nc = new NewClassw(); Type t = nc.GetType(); ConstructorInfo[] ci = t.GetConstructors(); //获取类的所有构造函数 foreach (ConstructorInfo c in ci) //遍历每一个构造函数 { ParameterInfo[] ps = c.GetParameters(); //取出每个构造函数的所有参数 foreach (ParameterInfo pi in ps) //遍历并打印所该构造函数的所有参数 { Console.Write(pi.ParameterType.ToString()+" "+pi.Name+","); } Console.WriteLine(); }
//用构造函数动态生成对象: Type t = typeof(NewClassw); Type[] pt = new Type[2]; pt[0] = typeof(string); pt[1] = typeof(string); //根据参数类型获取构造函数 ConstructorInfo ci = t.GetConstructor(pt); //构造Object数组,作为构造函数的输入参数 object[] obj = new object[2]{"grayworm","hi.baidu.com/grayworm"}; //调用构造函数生成对象 object o = ci.Invoke(obj); //调用生成的对象的方法测试是否对象生成成功 //((NewClassw)o).show();
//用Activator生成对象: Type t = typeof(NewClassw); //构造函数的参数 object[] obj = new object[2] { "grayworm", "hi.baidu.com/grayworm" }; //用Activator的CreateInstance静态方法,生成新对象 object o = Activator.CreateInstance(t,"grayworm","hi.baidu.com/grayworm"); //((NewClassw)o).show();
//查看类中的属性: NewClassw nc = new NewClassw(); Type t = nc.GetType(); PropertyInfo[] pis = t.GetProperties(); foreach(PropertyInfo pi in pis) { Console.WriteLine(pi.Name); }
//用反射生成对象,并调用属性、方法和字段进行操作 NewClassw nc = new NewClassw(); Type t = nc.GetType(); object obj = Activator.CreateInstance(t); //取得ID字段 FieldInfo fi = t.GetField("ID"); //给ID字段赋值 fi.SetValue(obj, "k001"); //取得MyName属性 PropertyInfo pi1 = t.GetProperty("MyName"); //给MyName属性赋值 pi1.SetValue(obj, "grayworm", null); PropertyInfo pi2 = t.GetProperty("MyInfo"); pi2.SetValue(obj, "hi.baidu.com/grayworm", null); //取得show方法 MethodInfo mi = t.GetMethod("show"); //调用show方法 mi.Invoke(obj, null);
9.public abstract class Assembly
AppDomain:应用程序域 Assembly:程序集类 Module:模块类 Type:使用反射得到类型信息的最核心的类
一个AppDomain可以包含N个Assembly,一个Assembly可以包含N个Module,而一个Module可以包含N个Type
Assembly,即程序集,简单地说就是一个dll或者一个exe,里面包含了很多类的定义和资源,可以通过反射获得程序集的信息。
a.通过程序集名称返回Assembly对象
Assembly ass = Assembly.Load("ClassLibrary831");
b.通过DLL文件名称返回Assembly对象
Assembly ass = Assembly.LoadFrom("ClassLibrary831.dll");
c.通过Assembly获取程序集中类
Type t = ass.GetType("ClassLibrary831.NewClass"); //参数必须是类的全名
d.通过Assembly获取程序集中所有的类
Type[] t = ass.GetTypes();
实例:
//通过程序集的名称反射 Assembly ass = Assembly.Load("ClassLibrary831"); Type t = ass.GetType("ClassLibrary831.NewClass"); object o = Activator.CreateInstance(t, "grayworm", "http://hi.baidu.com/grayworm"); MethodInfo mi = t.GetMethod("show"); mi.Invoke(o, null);
//通过DLL文件全名反射其中的所有类型 Assembly assembly = Assembly.LoadFrom("xxx.dll的路径"); Type[] aa = a.GetTypes(); foreach(Type t in aa) { if(t.FullName == "a.b.c") { object o = Activator.CreateInstance(t); } }
综合运用1:
新建一个项目,命名为TestAssembly
namespace TestAssembly { class Country { public string name; public void SetName(string name) { this.name = name; } public string GetName() { return name; } } }
using System; using System.Reflection; namespace TestAssembly { class Program { static void Main(string[] args) { String assemblyName = @"TestAssembly"; Assembly assembly = Assembly.Load(assemblyName); //Assembly assembly = typeof(Program).Assembly; foreach (Type type in assembly.GetTypes()) { Console.WriteLine(type.Name); FieldInfo[] fis = type.GetFields(); foreach (FieldInfo fi in fis) { Console.WriteLine(fi.Name); } Console.WriteLine(); } string strongClassName = assemblyName + ".Chinese"; Chinese c = (Chinese)assembly.CreateInstance(strongClassName); //Chinese c = Activator.CreateInstance(typeof(Chinese)) as Chinese; Console.WriteLine(c.name); Console.ReadLine(); } } class Chinese : Country { public Chinese() { name = "你好"; } } class America : Country { public America() { name = "Hello"; } } }
综合运用2:http://www.cnblogs.com/murongxiaopifu/p/4175395.html