每個.net程序集除了代碼外都額外包含了元數(shù)據(jù)。元數(shù)據(jù)包括了程序集本身的信息,比如版本號,引用了什么程序集,所有類型的信息,包括其方法、屬性、字段。使用.net反射,可以在運行時讀取這些信息,并且可以動態(tài)地調(diào)用方法。 項目快完了,終于有時間來寫blog了,  , 做一個動態(tài)調(diào)用程序集指定方法的例子。 項目1(Demo)中包含一個Test類, Test類中寫了一個 getList方法,這個方法返回的數(shù)據(jù)是手工加入的。源代碼如下:
 項目1
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;
}
}
}

項目2(DemoXml)中包含一個Test類, Test類中寫了一個 getList方法,這個方法返回的數(shù)據(jù)是從數(shù)據(jù)庫讀取的。源代碼如下:
 項目2
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;
}
}
}
}

項目3(WebDemo)中演示動態(tài)用指定程序集中 getList的方法返回一個DataTable,用一個gridview顯示其返回的數(shù)據(jù)。
 調(diào)用演示
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);
//返加程序集中的一個指定的對象,哪果是返回所有對象,則用GetTypes()返回一個Typt對象的數(shù)組.
Type T = assembly.GetType(NameSpace);
//返回方法信息(公共方法)
MethodInfo mi = T.GetMethod("getList");
//根據(jù)前面type類型創(chuàng)建一個對象
object o = Activator.CreateInstance(T);
//參數(shù)
 object[] par = new object[] { "E01" };
//通過MethodInfo對象的Invoke方法,動態(tài)調(diào)用此方法,參數(shù)o是因為實例方法需要在調(diào)用時有一個實例存在
DataTable dt = (DataTable)mi.Invoke(o, par);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
catch (Exception ex)
 {
//do Exception
}
}
}

通過Assembly.LoadFrom方法返回的Assembly對象,可以讀取其中的元數(shù)據(jù)。其中的GetType會返回一個用于表示指定程序集的type對象(讀取程序集中的所有類型用 GetTypes會返回一個type對象的數(shù)組)。 返回方法信息(公共方法) MethodInfo mi = T.GetMethod("getList"); 根據(jù)前面type類型創(chuàng)建一個對象 object o = Activator.CreateInstance(T); 參數(shù) object[] par = new object[] { "E01" }; 通過MethodInfo對象的Invoke方法,動態(tài)調(diào)用此方法,參數(shù)o是因為實例方法需要在調(diào)用時有一個實例存在. DataTable dt = (DataTable)mi.Invoke(o, par); 調(diào)用返回的數(shù)據(jù)顯示列表中。 示例下載
|