c# 动态加载dll文件

 /// <summary>
        /// 动态加载DLL
        /// </summary>
        /// <param name="lpFileName">DLL路径</param>
        /// <param name="Namespace">命名空间</param>
        /// <param name="ClassName">类名</param>
        /// <param name="lpProcName">公共函数名</param>
        /// <param name="ObjArray_Parameter"></param>
        /// <returns></returns>
        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(" 装载出错 !");
                    }
                }
            }
            catch (System.NullReferenceException e)
            {
                MessageBox.Show(e.Message);
            }
            return (object)0;
        }

猜你喜欢

转载自www.cnblogs.com/z45281625/p/10623716.html
今日推荐