动态调用程序集

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Reflection;

namespace BL_AutoGenerate
{
   public class BL_dllInvoke
    {
       
        object OBJ;//定义object对象
        Assembly MyAssembly;//要处理的程序及对象
        List<string> MyAssemblys;//定义字符串集
        #region 方法调用
        public object MethodInvoke(string lpFileName, string ClassName, string lpProcName, object[] ObjArray_Parameter)
        {
            try
            {
                string[] nameSpace = ClassName.Split(new char[]{'.'});//字符串处理
                if (MyAssembly == null || MyAssembly.GetName().Name != nameSpace[0])
                {
                    MyAssembly = Assembly.LoadFrom(lpFileName);//加载程序集                    
                }

                Type[] type = MyAssembly.GetTypes();//得到程序集类型

                foreach (Type t in type)
                {
                    if (ClassName.Trim() == t.ToString().Trim())//字符串相等
                    {                         
                        try
                        {
                            MethodInfo m = t.GetMethod(lpProcName);//方法信息
                            if (m != null)//不为空
                            {
                                if (OBJ == null || t.FullName != OBJ.ToString())
                                {
                                    OBJ = Activator.CreateInstance(t);//创建实例
                                }
                                return m.Invoke(OBJ, ObjArray_Parameter);//返回要调用的方法
                            }

                            else
                                MessageBox.Show(" 装载出错 !");//弹出错误信息
                        }
                        catch (Exception e)
                        {
                            //捕获异常
                            MessageBox.Show(e.Message.ToString());
                        }
                    }
                }
            }
            catch (System.NullReferenceException e)
            {
                //捕获异常
                MessageBox.Show(e.Message);
            }
            return (object)0;//返回
        }
        #endregion
        #region 构造函数调用
        public object ConstructorInvoke(string lpFileName, string ClassName,Type[] Constructor_Parameter, object[] ObjArray_Parameter)
        {
            try
            {
                //加载程序集
                MyAssembly = Assembly.LoadFrom(lpFileName);               
                //程序集类型
               Type[] type = MyAssembly.GetTypes();
                foreach (Type t in type)
                {// 查找要调用的命名空间及类 
                    if (ClassName.Trim() == t.ToString().Trim())
                    {// 查找要调用的方法并进行调用 
                        ConstructorInfo c = t.GetConstructor(Constructor_Parameter);//构造函数信息
                        if (c != null)
                        {
                            //创建构造函数对象
                            OBJ = c.Invoke(ObjArray_Parameter);
                            return OBJ;//返回对象
                        }
                        else
                            MessageBox.Show(" 装载出错 !");//显示出错信息
                    }
                }
            }
            catch (System.NullReferenceException e)
            {
                //输出异常信息
                MessageBox.Show(e.Message);
            }
            return (object)0;//返回空值
        }
        #endregion
        #region 获取成员变量
        public object GetField(string lpFileName,string ClassName, string FieldName)
        {
            try
            {
                //分解类名字符串,保存到nameSpace中
                string[] nameSpace = ClassName.Split(new char[] { '.' });                
                if ( MyAssembly == null || MyAssembly.GetName().Name != nameSpace[0])//如果为空且程序集的名字不等于ClassName
                {
                    MyAssembly = Assembly.Load(lpFileName);//加载程序集
                }
                Type[] type = MyAssembly.GetTypes();//获取此程序集中定义的类型
                foreach (Type t in type)
                {// 查找要调用的命名空间及类                     
                    if (ClassName.Trim() == t.ToString().Trim())
                    {// 得到属性信息
                        FieldInfo f = t.GetField(FieldName);
                        if (f != null)
                        {
                            if (OBJ == null || t.FullName != OBJ.ToString())
                            {
                                OBJ = Activator.CreateInstance(t);//创建实例
                            }
                            return f.GetValue(OBJ);//返回成员变量值
                        }
                        else
                            MessageBox.Show(" 装载出错 !");//显示错误信息
                    }
                }
            }
            catch (System.NullReferenceException e)
            {//捕获异常
                MessageBox.Show(e.Message);
            }
            return (object)0;//返回空值
        }
        #endregion
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值