All articles| All Pictures| All Softwares| All Video| Go home page| Write articles| Upload pictures

Reading number is top 10 articles
实用技巧,利用Apache实现禁止图片盗链_php资料_编程技术
Delphi在状态栏中显示检查框实例
visual c++定制状态栏
区别和认识.Net四个判等函数_.net资料_编程技术
轻松掌握Ajax.net系列教程十四:使用CalendarExtender_[Asp.Net教程]
Asp.Net小技巧之在client端调用server端事件_.net资料_编程技术
如何在十天内学会php之第十天_[PHP教程]
SQL,Server,2008,R2,特色功能盘点_mssql学习_编程技术
打造Ajax简单相册_[AJAX教程]
Sqlserver负载测试性能调整的魔法_[SQL,Server教程]
Reading number is top 10 pictures
联通的3G无线网卡我只用了一天,看看流量......
Average female college students3
美丽的桂林风光2
Summer is most suitable for young people to travel in China5
2012 national geographic daily picture1
Flow chart of breast implants
Sell the barbecue as says father du breul1
Born After 90 Beijing sports university campus flower photos1
八个盛产美女的国家1
29 the belle stars after bath figure5
Download software ranking
美女写真2
Sora aoi's film--cangkong_Blue.Sky
Unix video tutorial15
jdk1.6 for windows
JSP+Ajax Web development typical examples
dreamweaver8中文版
linux初级教程
Boxer's Top ten classic battle2
Tram sex maniac 2 (H) rar bag14
Tram sex maniac 2 (H) rar bag7
delv published in(发表于) 2014/1/10 6:30:41 Edit(编辑)
利用.net反射动态调用指定程序集的中的方法_[Asp.Net教程]

利用.net反射动态调用指定程序集的中的方法_[Asp.Net教程]

利用.net反射动态调用指定程序集的中的方法_[Asp.Net教程]


每个.net程序集除了代码外都额外包含了元数据。元数据包括了程序集本身的信息,比如版本号,引用了什么程序集,所有类型的信息,包括其方法、属性、字段。使用.net反射,可以在运行时读取这些信息,并且可以动态地调用方法。
项目快完了,终于有时间来写blog了,,
做一个动态调用程序集指定方法的例子。
项目1(Demo)中包含一个Test类,Test类中写了一个getList方法,这个方法返回的数据是手工加入的。源代码如下:
项目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方法,这个方法返回的数据是从数据库读取的。源代码如下:
项目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("出现异常:"+ex.Message+ex.StackTrace);
}
finally
{
cn.Close();
cn = null;
}
}
}
}


项目3(WebDemo)中演示动态用指定程序集中getList的方法返回一个DataTable,用一个gridview显示其返回的数据。
调用演示
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();
}
}
数据初始化,可配置在web.config文件中#region 数据初始化,可配置在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"] = "加载自己定义数据";
dr["filepath"] = Server.MapPath(@"Files\Demo.dll");
dt.Rows.Add(dr);
dr = dt.NewRow();
dr["name"] = "加载xml数据";
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";
//加载指定的程序集之内存中
Assembly assembly = Assembly.LoadFrom(strPath);
//返加程序集中的一个指定的对象,哪果是返回所有对象,则用GetTypes()返回一个Typt对象的数组.
Type T = assembly.GetType(NameSpace);
//返回方法信息(公共方法)
MethodInfo mi = T.GetMethod("getList");
//根据前面type类型创建一个对象
object o = Activator.CreateInstance(T);
//参数
object[] par = new object[] { "E01" };
//通过MethodInfo对象的Invoke方法,动态调用此方法,参数o是因为实例方法需要在调用时有一个实例存在
DataTable dt = (DataTable)mi.Invoke(o, par);
this.GridView1.DataSource = dt;
this.GridView1.DataBind();
}
catch (Exception ex)
{
//do Exception
}
}
}


通过Assembly.LoadFrom方法返回的Assembly对象,可以读取其中的元数据。其中的GetType会返回一个用于表示指定程序集的type对象(读取程序集中的所有类型用GetTypes会返回一个type对象的数组)。
返回方法信息(公共方法)
MethodInfo mi = T.GetMethod("getList");
根据前面type类型创建一个对象
object o = Activator.CreateInstance(T);
参数
object[] par = new object[] { "E01" };
通过MethodInfo对象的Invoke方法,动态调用此方法,参数o是因为实例方法需要在调用时有一个实例存在.
DataTable dt = (DataTable)mi.Invoke(o, par);
调用返回的数据显示列表中。
示例下载:http://www.cnblogs.com/Files/NetFans/Solution2.rar


来源:博客园







添加到del.icio.us 添加到新浪ViVi 添加到百度搜藏 添加到POCO网摘 添加到天天网摘365Key 添加到和讯网摘 添加到天极网摘 添加到黑米书签 添加到QQ书签 添加到雅虎收藏 添加到奇客发现 diigo it 添加到饭否 添加到飞豆订阅 添加到抓虾收藏 添加到鲜果订阅 digg it 貼到funP 添加到有道阅读 Live Favorites 添加到Newsvine 打印本页 用Email发送本页 在Facebook上分享


Disclaimer Privacy Policy About us Site Map

If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.