动态解析dll及使用类

/// <summary>
        /// 动态解析dll
        /// </summary>
        public object DomainSerializer(Module _Module)
        {
            object ReturnValue = null;
            int state = 0;
            try
            {
                //获取类库的绝对路径
                string path = Server.MapPath("bin\" + _Module.DLLNAME + ".dll");

                Assembly assembly = Assembly.LoadFile(path);
                bool isExistsClass = false;
                foreach (Type pluginType in assembly.GetTypes())
                {
                    if (!pluginType.IsPublic || pluginType.IsAbstract || pluginType.IsInterface)
                        continue;

                    //判断参数是否一致及类名是否相同
                    if (pluginType.Name == _Module.CLASSNAME)
                    {
                        object obj = System.Activator.CreateInstance(pluginType);
                        MethodInfo Mymethodinfoa = pluginType.GetMethod(_Module.FUNNAME);
                        ParameterInfo[] Parameter = Mymethodinfoa.GetParameters();
                        if (Parameter.Count() == _Module.DICTION.Count())
                        {
                            isExistsClass = true;
                            state = 1;
                            ReturnValue = Mymethodinfoa.Invoke(obj, _Module.DICTION);
                        }
                    }
                }
                if (!isExistsClass)
                    ReturnValue="99类库" + _Module.DLLNAME + "中不存在" + _Module.CLASSNAME + "!";
            }
            catch (Exception ex)
            {
                if (state == 0)
                    ReturnValue = "99类库" + _Module.DLLNAME + "加载失败!" + ex.Message;
                else if (state == 1)
                    ReturnValue = "99"+_Module.CLASSNAME + "中方法参数不一致!" + ex.Message;
            }
            return ReturnValue;
        }
更多技术交流+QQ:318617848
原文地址:https://www.cnblogs.com/huyaguang/p/5888847.html