动态

来源:互联网 发布:守望先锋 英雄详细数据 编辑:程序博客网 时间:2024/04/23 18:47
using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Windows.Forms;using System.Reflection;public class BL_DllInvoke  {    object OBJ;//object对象    Assembly MyAssembly;//要处理的程序及对象    List<string> MyAssemblys;//字符串集    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;//返回    }    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;//返回空值        }    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;//返回空值    }}


原创粉丝点击