一、获取程序集Assembly

1、获取当前运行的程序集

System.Reflection.Assembly[] asm = AppDomain.CurrentDomain.GetAssemblies();
//
Assembly b = Assembly.GetExecutingAssembly();

2、获取指定文件的程序集:Load,LoadFrom,LoadFile方法。

Assembly c = Assembly.Load("mscorlib.dll");//如果你引用了程序及,那么就直接Load()方法,参数里面程序集名称就可以加载了。Assembly c = Assembly.Load("mscorlib");
Assembly d = Assembly.LoadFrom(AppDomain.CurrentDomain.BaseDirectory + "mscorlib.dll");//LoadFrom只能用于加载不同标识的程序集, 也就是唯一的程序集, 不能用于加载标识相同但路径不同的程序集。
Assembly e = Assembly.LoadFile(AppDomain.CurrentDomain.BaseDirectory + "mscorlib.dll");//LoadFile:只加载指定文件,但不会自动加载依赖程序集

二、获取类型Type (指Class类):

Assembly asm = Assembly.GetExecutingAssembly();
Type[] tArr = asm.GetExportedTypes();//获取程序集中定义的公共类型

1、从类字符串中获得Type对象:Assembly.GetType(“”),Module.GetType(“”), Type.GetType(“”)

Assembly ass = Assembly.LoadFrom(@"C:\bin\Debug\ConsoleApplication2.exe");
Console.WriteLine(ass.GetType("ConsoleApplication2.Person").ToString()); //根据程序集(dll或exe)获取里面的Class Module mod = ass.GetModules()[0];
Console.WriteLine(mod.GetType("ConsoleApplication2.Person").ToString()); Type type = Type.GetType("System.Int32");//静态方法,参数为完全限定名(首选)
Type type = Type.GetType("MyAssembly.Example",false,true) //注意0是类名,参数1表示若找不到对应类时是否抛出异常,参数2表示类名是否区分大小写

2、从具体类中获得Type对象:typeof运算符

Type t4 = typeof(TestSpace.TestClass);//使用typeof运算符

3、从实例中获得Type对象:Object.GetType()

Example example = new Example();
Type type = example.GetType(); Type t3 = 42.GetType();//根据对象实例获取类型

4、Type的属性

t.IsPublic;
t.IsAbstract;
t.IsClass;
t.IsValueType;

三、获取成员MemberInfo

MemberInfo[] miArr = t.GetMembers(BindingFlags.Instance | BindingFlags.Public);//实例与公共成员。还有BindingFlags.Static|BindingFlags.NonPublic
foreach (MemberInfo item in miArr)
{
bool a = item is FieldInfo;
PropertyInfo;
MethodBase;
ConstructorInfo;
MethodInfo;
EventInfo;
Type;
} t.GetConstructor();//获取构造函数
t.GetFields();//获取字段
t.GetProperties(); //获取属性
t.GetMethods();//获取方法
t.GetEvents();//获取事件
t.GetInterfaces();//获取接口
t.GetCustomAttributes(true);//获取类型上标记的自定义属性

在System.Reflection命名空间内包含多个反射常用的类:

  • Assembly: 通过此类可以加载操纵一个程序集,并获取程序集内部信息
  • EventInfo: 该类保存给定的事件信息
  • FieldInfo :该类保存给定的字段信息
  • MethodInfo :该类保存给定的方法信息
  • MemberInfo :该类是一个基类,它定义了EventInfo、FieldInfo、MethodInfo、PropertyInfo的多个公用行为
  • Module :该类可以使你能访问多个程序集中的给定模块
  • ParameterInfo :该类保存给定的参数信息
  • PropertyInfo: 该类保存给定的属性信息

四、获取具体成员

Type t = Assembly.GetExecutingAssembly().GetType("TestSpace.TestClass");
MethodInfo m = t.GetMethod("TestMethod");
ParameterInfo[] p = m.GetParameters();//获取方法参数

五、创建实例

1、根据Assembly创建类型实例:asm.CreateInstance()

Assembly asm = Assembly.GetExecutingAssembly();
TestClass obj = asm.CreateInstance("TestSpace.TestClass");//根据Assembly创建类型实例

2、根据类型创建实例:Activator.CreateInstance()

Type t = Type.GetType("TestSpace.TestClass");
TestClass obj = (TestClass)Activator.CreateInstance(t);//1、根据类型创建实例
TestClass obj = (TestClass)Activator.CreateInstance(t, new object[] { "aa" });// 2、根据”有参数的构造函数”创建实例
//
TestClass obj = (TestClass)t.InvokeMember("TestClass", BindingFlags.CreateInstance, null, null, null);

六、调用方法

1、调用实例方法:Invoke

MethodInfo m = t.GetMethod("WriteString");
object returnValue = m.Invoke(obj, new object[] { "test", 1 });//传两参数,若方法无参数,可以将Invoke的第二个参数设为null
//或者
object returnValue = m.Invoke(obj, BindingFlags.Public, Type.DefaultBinder, new object[] { "test", 1 }, null);//最后一个参数表示Culture.

2、调用静态方法

MethodInfo m = t.GetMethod("StaticMethod");
object returnValue = m.Invoke(null, new object[] { "test", 1 });

七、反射属性

通过System.Reflection.Property能查找到类里面的属性。 常用的方法有GetValue(object,object[])获取属性值和SetValue(object,object,object[])设置属性值

PropertyInfo propertyName = type.GetProperty("Name");    //获取Name属性对象
propertyName.SetValue(obj, "张飞", null); //设置Name属性的值
object objName = propertyName.GetValue(obj, null); //获取属性值

根据属性的类型设置属性的值

Type type = typeof(Person);      //注意要输入全部路径,包括命名空间
object obj = Activator.CreateInstance(type);
//假设这是存在于XML的数据
Dictionary<string, string> dic = new Dictionary<string, string>();
dic.Add("Id", "1");
dic.Add("Name", "神灵武士");
dic.Add("Birthday", "2001-01-01"); PropertyInfo[] ProArr = type.GetProperties();
foreach (PropertyInfo p in ProArr)
{
if (dic.Keys.Contains(p.Name))
{
p.SetValue(obj, Convert.ChangeType(dic[p.Name], p.PropertyType), null); //当需要给属性设置不同类型的值时
}
} Person person = obj as Person;
Console.WriteLine(person.Birthday);

八、反射特性

通过System.Reflection.MemberInfo的GetCustomAttributes(Type,bool)就可反射出一个类里面的特性。

Assembly assembly = Assembly.Load("fanshe");
Type type = assembly.GetType("fanshe.Person"); //注意要输入全部路径,包括命名空间
object obj = Activator.CreateInstance(type);
object[] typeAttributes = type.GetCustomAttributes(false); //获取Person类的特性
foreach (object attribute in typeAttributes)
{
Console.WriteLine(attribute.ToString()); //输出 System.SerializableAttribute 因为我在Person上里加了个[Serializable]
}

九、创建委托实例

TestDelegate myDelegate = (TestDelegate)Delegate.CreateDelegate(typeof(TestDelete), obj, "MyMethod");
string returnValue = myDelegate("Hello");//执行委托

十、应用举例

1、动态加载程序集

Assembly asm = Assembly.LoadFrom(@"E:\Test.dll");
Type type = asm.GetType("TestSpace.TestClass");
object obj = System.Activator.CreateInstance(type);//也可以使用强制转换,将obj转换为预定义的接口或者抽象类(如Form),直接执行基方法,不用反射GetMethod .
MethodInfo m = type.GetMethod("WriteString");
m.Invoke(obj, new object[] { "test" });

2、获得List<T>中的T类型:

List<Dog> dogs = new List<Dog>();
Type type = dogs.GetType();
if (type.IsGenericType)
{
  Type[] genericArgTypes = type.GetGenericArguments();
  if (genericArgTypes[0] == typeof(Dog))
  {
  //你想要判断的是这个吗?
  }
}

最新文章

  1. 如何使用一个对象而非数组元素为ng-options初始化
  2. 论Postgres的“已提交的而且 xmin’比当前事务的XID小的记录对当前事务才是可见的”
  3. Android项目实战(十二):解决OOM的一种偷懒又有效的办法
  4. py继续
  5. 3DES 加解密,对长度不限制
  6. 在QuickReport中实现多栏打印
  7. javascript 数组 排除null, undefined, 和不存在的元素
  8. iOS 数字每隔3位添加一个逗号的
  9. 记一次SAP新业务开发项目
  10. [H5]range对象之selectNode等方法
  11. 【python】函数返回值
  12. iOS控制反转(IoC)与依赖注入(DI)的实现
  13. C# 今天时间 今天结束时间
  14. MATLAB R2017a 进入主界面以后一直处于初始化状态的解决办法
  15. tomcat接口调用时延开关
  16. A1052. Linked List Sorting
  17. 安装beanstalkd
  18. &#39;Agent XPs&#39; component is turned off as part of the security configuration for this server
  19. 自我简介与Github的注册和使用
  20. HTML5 Geolocation位置信息定位总结

热门文章

  1. @ResponseBody使用
  2. 深度图转伪彩色图(python)
  3. [转帖]详解JVM内存布局及GC原理,值得收藏
  4. 2019php面试大全
  5. Anaconda安装报错
  6. Mysql分表和分区的区别、分库和分表区别
  7. poj 2915
  8. Java ShellSort
  9. ASP.NET WEB应用程序(.network4.5)MVC Razor视图引擎2 视图模板页
  10. my SO 链接opencv静态库一些FUCKing的笔记 opencv410 有毒