您的位置:首页 > 其它

简单的反射例子及调用方法

2009-06-05 17:52 429 查看

简单的反射例子及调用方法

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;
using System.Data.SqlClient;

namespace domain.UI.News.Controls
{
public partial class Index : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

// Assembly a = Assembly.LoadFrom(@"D:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\反射\ClassLibrary1\bin\Debug\ClassLibrary1.dll");//类库的存储路径
Assembly a = Assembly.LoadFrom(HttpRuntime.BinDirectory + @"domain.UI.dll");//类库的存储路径
Type type = a.GetType("domain.UI.News.Controls.TestAssembly");//此处必须为类的完整名称

Object o = Activator.CreateInstance(type);//实例化类
foreach (MemberInfo mi in type.GetMethods())
{
Response.Write(mi.Name+"<br>");
}
MethodInfo method_add = type.GetMethod("add");//得到方法的信息
string i = (string)method_add.Invoke(o, new object[] { 1, new int[] { 2, 6 }, new SqlParameter("@C_ID", "wangdetian"), new string[] { "12","11"} });//实现方法
Response.Write(i);
}
}
public class TestAssembly
{
public string add(int i,int[] j,SqlParameter pas,params string[] aaa)
{
return (i + j[1]).ToString()+pas.Value.ToString()+aaa.Length;

}
//public string add(int i, int j,int z)
//{
// return (i + j+z).ToString();
//}

}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: