Пример #1
0
 /// <summary> 注册资源中的dll
 /// </summary>
 /// <param name="pattern">*表示连续的未知字符,_表示单个未知字符,如*.dll</param>
 public static void RegistDLL(string pattern = "*.dll")
 {
     System.IO.Directory.GetFiles("", "");
     //获取调用者的程序集
     var ass = new StackTrace(0).GetFrame(1).GetMethod().Module.Assembly;
     //判断程序集是否已经处理
     if (Assemblies.ContainsKey(ass.FullName))
     {
         return;
     }
     //程序集加入已处理集合
     Assemblies.Add(ass.FullName, null);
     //绑定程序集加载失败事件(这里我测试了,就算重复绑也是没关系的)
     AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolve;
     //获取所有资源文件文件名
     var res = ass.GetManifestResourceNames();
     var regex = new Regex("^" + pattern.Replace(".", "\\.").Replace("*", ".*").Replace("_", ".") + "$", RegexOptions.IgnoreCase);
     foreach (var r in res)
     {
         //如果是dll,则加载
         if (regex.IsMatch(r))
         {
             try
             {
                 var s = ass.GetManifestResourceStream(r);
                 var bts = new byte[s.Length];
                 s.Read(bts, 0, (int)s.Length);
                 var da = Assembly.Load(bts);
                 //判断是否已经加载
                 if (LoadedDlls.ContainsKey(da.FullName))
                 {
                     continue;
                 }
                 LoadedDlls[da.FullName] = da;
             }
             catch
             {
                 //加载失败就算了...
             }
         }
     }
 }
Пример #2
0
 /// <summary> 注册资源中的dll
 /// </summary>
 public static void RegistDLL()
 {
     //获取调用者的程序集
     var ass = new StackTrace(0).GetFrame(1).GetMethod().Module.Assembly;
     //判断程序集是否已经处理
     if (Assemblies.ContainsKey(ass.FullName))
     {
         return;
     }
     //程序集加入已处理集合
     Assemblies.Add(ass.FullName, null);
     //绑定程序集加载失败事件(这里我测试了,就算重复绑也是没关系的)
     AppDomain.CurrentDomain.AssemblyResolve += AssemblyResolve;
     //获取所有资源文件文件名
     var res = ass.GetManifestResourceNames();
     foreach (var r in res)
     {
         //如果是dll,则加载
         if (r.EndsWith(".dll", StringComparison.OrdinalIgnoreCase))
         {
             try
             {
                 var s = ass.GetManifestResourceStream(r);
                 var bts = new byte[s.Length];
                 s.Read(bts, 0, (int)s.Length);
                 var da = Assembly.Load(bts);
                 //判断是否已经加载
                 if (Dlls.ContainsKey(da.FullName))
                 {
                     continue;
                 }
                 Dlls[da.FullName] = da;
             }
             catch
             {
                 //加载失败就算了...
             }
         }
     }
 }