zoukankan      html  css  js  c++  java
  • 动态加载及调用程序集


    static DbCmd()
            {
                AppDomain currentDomain = AppDomain.CurrentDomain;
                currentDomain.AssemblyResolve += new ResolveEventHandler(MyResolveEventHandler);
    
            }
            static Assembly MyResolveEventHandler(object sender, ResolveEventArgs args)
            {
                string strFielName = args.Name.Split(',')[0];
                if (strFielName.ToLower().EndsWith(".resources"))  //不处理资源文件
                    return null;
                ExtMethod.WriteLog("DbCmd 程序集获取失败,准备动态加载:" + args.Name);
                string parentRoot = AppDomain.CurrentDomain.BaseDirectory;
                string dllName = GetDllOrExeName(parentRoot, strFielName);
                if (dllName == "")
                {
                    parentRoot = System.IO.Path.GetDirectoryName(AppDomain.CurrentDomain.BaseDirectory.TrimEnd('\'));
                    dllName = GetDllOrExeName(parentRoot, strFielName);
                }
                if (dllName != null)
                {
                    ExtMethod.WriteLog("DbCmd 程序集开始加载文件:" + dllName);
                    return Assembly.LoadFile(dllName);
                }
                else
                {
                    ExtMethod.WriteLog("DbCmd 程序集获取失败:找不到文件" + strFielName);
                }
                return null;
            }
            /// <summary>
            /// 获取指定名称的dll路径
            /// </summary>
            /// <param name="_DirectoryName">目录路径</param>
            /// <param name="_assemablName">程序集名称</param>
            /// <returns>返回路径名称</returns>
            static string GetDllOrExeName(string _DirectoryName, string _assemablName)
            {
                string filter = _assemablName + ".???";
                var subFiles = System.IO.Directory.GetFiles(_DirectoryName, filter, System.IO.SearchOption.AllDirectories);
                string dllName = "";
                foreach (var item in subFiles) //搜索下级目录
                {
                    if (item.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
                    {
                        dllName = item;
                        break;
                    }
                    if (item.EndsWith(".exe", StringComparison.OrdinalIgnoreCase))
                    {
                        dllName = item;
                        break;
                    }
                }
                return dllName;
            }
  • 相关阅读:
    MySQL数据库表的设计和优化(上)
    MySQL性能优化最佳实践20条
    MySQL高性能优化指导思路
    MySQL 5.6 my.cnf优化后的标准配置(4核 16G Centos6.5 x64)
    MySQL优化之索引优化
    MySQL优化之SQL语句优化
    MySQL优化之配置参数调优
    Apache的ab测试
    FastCGI模式下安装Xcache
    除了用作缓存数据,Redis还可以做这些
  • 原文地址:https://www.cnblogs.com/hdl217/p/14281532.html
Copyright © 2011-2022 走看看