动态加载dll(比如webapi)

贴下码:

 const string signpath = "webapi"; //应用程序根目录下的webapi文件夹中存放api文件
            const string signfile = ".dll";   //加载的pai文件的扩展名须是dll

            ICollection<Assembly> baseAssemblies = base.GetAssemblies();
            List<Assembly> assemblies = new List<Assembly>(baseAssemblies);

            var apipath = AppDomain.CurrentDomain.BaseDirectory + string.Format("\\{0}\\", signpath);

            if (Directory.Exists(apipath))
            {
                var apifiles = Directory.GetFiles(apipath, "*" + signfile)
                                .Where(t => t.ToLower().EndsWith(signfile)) //过滤
                                .ToList();
                foreach (var apidll in apifiles)
                {
                    baseAssemblies.Add(Assembly.LoadFrom(apidll));
                }
            }
            return assemblies;
然后使用 HttpSelfHostServer 来自宿主,加载程序集


你可能感兴趣的:(动态加载DLL)