-
引言
- 在类中实例化其他类,通常称为程序的耦合。程序应追求高内聚,低耦合;反射可以降低耦合。
- .net中编译后生成的东西,都能称为程序集,比如dll,exe文件。
- 通过添加程序集,引用命名空间,可以直接使用程序集的中元素。如果不添加程序和引用命名空间,想使用程序集中的元素,可以通过反射的方式。
-
反射
-
反射的三种方式
//只要程序集的名称 Assembly assembly1 = Assembly.Load("ReflectL"); Type type = assembly1.GetType(); //需要程序集的完整名称,包括扩展名 Assembly assembly2 = Assembly.LoadFrom("ReflectL.dll"); string path = AppDomain.CurrentDomain.BaseDirectory + "ReflectL.dll"; //需要程序集的完整路径 Assembly assembly3 = Assembly.LoadFile(path); Type type1 = assembly3.GetType();