C#技术百科
问问你的心你有没有信心 做事情要专一坚定,执着

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

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();
        //}

    }
}

posted on 2009-06-05 17:52  王德田  阅读(260)  评论(0编辑  收藏  举报