清风的blog 优然探索

asp.net动态调用DLL文件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using System.Reflection;
using System.Runtime.Remoting;
namespace MyDLLDyUserd
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            object[] args = new object[0];
            MessageBox.Show(" 这是您第 " + Invoke("MyTestClass.dll", "MyTestClass", "Class1", "getDate", args));
       
        }

 

        private object Invoke(string lpFileName, string Namespace, string ClassName, string lpProcName, object[] ObjArray_Parameter)
        {
            try
            { // 载入程序集
                Assembly MyAssembly = Assembly.LoadFrom(lpFileName);
                Type[] type = MyAssembly.GetTypes();
                foreach (Type t in type)
                {// 查找要调用的命名空间及类
                    if (t.Namespace == Namespace && t.Name == ClassName)
                    {// 查找要调用的方法并进行调用
                        MethodInfo m = t.GetMethod(lpProcName);
                        if (m != null)
                        {
                            object o = Activator.CreateInstance(t);
                            return m.Invoke(o, ObjArray_Parameter);
                        }
                        else MessageBox.Show(" 装载出错 !");
                    }
                }
            }//try
            catch (System.NullReferenceException e)
            {
                MessageBox.Show(e.Message);
            }//catch
            return (object)0;
        }// Invoke

      
    }
}
 

2010年1月19日 | 发布:admin | 分类:技术文章 | 评论:0

发表留言: