using System; using System.Data; using System.Configuration; using System.Collections; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; using System.Reflection; //反射必须的命名空间 public partial class Default3 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { //创建一个程序集的实例,使用加载方法加载程序集(为程序集的物理路径) Assembly obj = Assembly.LoadFrom(@"E:/dufy/CustomerSystem/CustomerSystem.DataAccess/bin/Debug/CustomerSystem.DataAccess.dll"); //声明一个泛类型数组,取得程序集中的类的集合 Type[] myType = obj.GetTypes(); //循环出程序集中所有的类 for (int i = 0; i < myType.Length; i++) { Response.Write(myType[i] + "<br><br><br>"); //声明获得方法的数组,获得某个类下方法的集合 MethodInfo[] methods = myType[i].GetMethods(); for (int j = 0; j < methods.Length; j++) { Response.Write(methods[j] + "<br><br>"); //声明一个获得参数的数组,获得某个方法下面所有参数 ParameterInfo[] pare = methods[j].GetParameters(); for (int k = 0; k < pare.Length; k++) { Response.Write(pare[k] + "<br>"); } } } //调用方法 //声明一个泛类型,获得程序集中的某个类GetType("程序集中的某个类的类名") Type t = obj.GetType(myType[2].ToString()); //创建一个程序集中类的实例 Object myObj = Activator.CreateInstance(t); //创建一个程序集中类下的方法的实例 MethodInfo mi = t.GetMethod("ShowCategory"); DataSet ds = new DataSet(); //调用程序集中类的方法 ds = mi.Invoke(myObj, null) as DataSet; GridView1.DataSource = ds; GridView1.DataBind(); //反射调用一个接收两个int类型,返回一个int类型的方法 //myType[4].ToString()其实就是类名 Type addT = obj.GetType(myType[4].ToString()); Object newObj = Activator.CreateInstance(addT); //类下的方法 MethodInfo add = addT.GetMethod("Add"); //类需要的参数,是一个类类型的数组,所以要把int型的参数放在类类型的数组中 Object[] o = new Object[] { 5, 6 }; Response.Write(add.Invoke(newObj, o).ToString()); } }