|
每個(gè).net程序集除了代碼外都額外包含了元數(shù)據(jù)。元數(shù)據(jù)包括了程序集本身的信息,比如版本號(hào),引用了什么程序集,所有類型的信息,包括其方法、屬性、字段。使用.net反射,可以在運(yùn)行時(shí)讀取這些信息,并且可以動(dòng)態(tài)地調(diào)用方法。
項(xiàng)目快完了,終于有時(shí)間來(lái)寫blog了, ,做一個(gè)動(dòng)態(tài)調(diào)用程序集指定方法的例子。 項(xiàng)目1(Demo)中包含一個(gè)Test類,Test類中寫了一個(gè)getList方法,這個(gè)方法返回的數(shù)據(jù)是手工加入的。源代碼如下: using System; using System.Collections.Generic; using System.Text; using System.Data;![]() namespace Demo![]() ![]() { public class Test![]() { public DataTable getList(string id)![]() { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("id")); dt.Columns.Add(new DataColumn("name")); dt.Columns.Add(new DataColumn("sex")); DataRow dr = dt.NewRow(); dr["id"] = "zl"; dr["name"] = "張鈴"; dr["sex"] = "男"; dt.Rows.Add(dr); dr = dt.NewRow(); dr["id"] = "zl"; dr["name"] = "李四"; dr["sex"] = "女"; dt.Rows.Add(dr); return dt; } } }![]() 項(xiàng)目2(DemoXml)中包含一個(gè)Test類,Test類中寫了一個(gè)getList方法,這個(gè)方法返回的數(shù)據(jù)是從數(shù)據(jù)庫(kù)讀取的。源代碼如下: using System; using System.Collections.Generic; using System.Text; using System.Data; using System.Data.SqlClient; using System.Xml; namespace DemoXml![]() ![]() { public class Test![]() { private SqlConnection cn; public DataTable getList(string id)![]() { try![]() { cn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["pubs"]); SqlCommand cmd = new SqlCommand(); SqlDataAdapter da = new SqlDataAdapter(); cmd.CommandText = "SELECT au_id as id,au_lname as name,au_fname as sex from authors"; cmd.CommandType = CommandType.Text; cmd.Connection = cn; da.SelectCommand = cmd; DataTable dt = new DataTable(); da.Fill(dt); return dt; } catch (Exception ex)![]() { throw new ApplicationException("出現(xiàn)異常:"+ex.Message+ex.StackTrace); } finally![]() { cn.Close(); cn = null; } } } }![]() 項(xiàng)目3(WebDemo)中演示動(dòng)態(tài)用指定程序集中getList的方法返回一個(gè)DataTable,用一個(gè)gridview顯示其返回的數(shù)據(jù)。 using System; using System.Data; using System.Configuration; 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 _Default : System.Web.UI.Page ![]() ![]() { protected void Page_Load(object sender, EventArgs e)![]() { if (!IsPostBack)![]() { DropBind(); } }![]() 數(shù)據(jù)初始化,可配置在web.config文件中#region 數(shù)據(jù)初始化,可配置在web.config文件中 public void DropBind()![]() { DataTable dt = new DataTable(); dt.Columns.Add(new DataColumn("name")); dt.Columns.Add(new DataColumn("filepath")); DataRow dr = dt.NewRow(); dr["name"] = "加載自己定義數(shù)據(jù)"; dr["filepath"] = Server.MapPath(@"Files\Demo.dll"); dt.Rows.Add(dr); dr = dt.NewRow(); dr["name"] = "加載xml數(shù)據(jù)"; dr["filepath"] = Server.MapPath(@"Files\DemoXml.dll"); dt.Rows.Add(dr); this.DropDownList1.DataSource = dt; this.DropDownList1.DataTextField = "name"; this.DropDownList1.DataValueField = "filepath"; this.DropDownList1.DataBind(); } #endregion![]() protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)![]() { try![]() { //讀取選擇指定的dll文件 string strPath = (sender as DropDownList).SelectedValue.Trim(); string NameSpace = this.DropDownList1.SelectedIndex == 0 ? "Demo.Test" : "DemoXml.Test"; //加載指定的程序集之內(nèi)存中 Assembly assembly = Assembly.LoadFrom(strPath); //返加程序集中的一個(gè)指定的對(duì)象,哪果是返回所有對(duì)象,則用GetTypes()返回一個(gè)Typt對(duì)象的數(shù)組. Type T = assembly.GetType(NameSpace); //返回方法信息(公共方法) MethodInfo mi = T.GetMethod("getList"); //根據(jù)前面type類型創(chuàng)建一個(gè)對(duì)象 object o = Activator.CreateInstance(T); //參數(shù)![]() object[] par = new object[] { "E01" }; //通過MethodInfo對(duì)象的Invoke方法,動(dòng)態(tài)調(diào)用此方法,參數(shù)o是因?yàn)閷?shí)例方法需要在調(diào)用時(shí)有一個(gè)實(shí)例存在 DataTable dt = (DataTable)mi.Invoke(o, par); this.GridView1.DataSource = dt; this.GridView1.DataBind(); } catch (Exception ex)![]() { //do Exception } } }![]() 通過Assembly.LoadFrom方法返回的Assembly對(duì)象,可以讀取其中的元數(shù)據(jù)。其中的GetType會(huì)返回一個(gè)用于表示指定程序集的type對(duì)象(讀取程序集中的所有類型用GetTypes會(huì)返回一個(gè)type對(duì)象的數(shù)組)。 返回方法信息(公共方法) MethodInfo mi = T.GetMethod("getList"); 根據(jù)前面type類型創(chuàng)建一個(gè)對(duì)象 object o = Activator.CreateInstance(T); 參數(shù) object[] par = new object[] { "E01" }; 通過MethodInfo對(duì)象的Invoke方法,動(dòng)態(tài)調(diào)用此方法,參數(shù)o是因?yàn)閷?shí)例方法需要在調(diào)用時(shí)有一個(gè)實(shí)例存在. DataTable dt = (DataTable)mi.Invoke(o, par); 調(diào)用返回的數(shù)據(jù)顯示列表中。 示例下載 |
|
|