Exemplo n.º 1
0
        /// <summary>
        /// 随着项目规模的逐渐扩大,项目引用的dll也越来越多,这些dll默认情况下全部都需要放在跟主程序相同的目录下,
        /// dll一多,主程序的目录就会显得非常凌乱。那么有没有什么办法可以把dll放到其他目录下也能正确加载呢,答案是肯定的,就是利用AppDomain的AssemblyResolve事件。
        /// AssemblyResolve事件在.Net对程序集的解析失败时触发,返回一个Assembly对象。因此,我们只要在这个事件的处理程序里手动加载对应目录的dll,
        /// 并把对应dll的Assembly对象返回,.Net就能正确加载对应的dll了。
        /// 使用AssemblyResolve事件除了本文介绍的,能在任意目录加载程序集外,应该还可以从其他特殊的地方加载程序集
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="resolveEventArgs"></param>
        /// <returns></returns>
        private static Assembly AssemblyResolve(object obj, ResolveEventArgs resolveEventArgs)
        {
            AppDomain appDomain = (AppDomain)obj;
            string    text      = appDomain.ApplyPolicy(resolveEventArgs.Name);
            Assembly  assembly  = null;

            try
            {
                if (text != resolveEventArgs.Name)
                {
                    assembly = Assembly.Load(text);
                }
                else
                {
                    assembly = AssemblyUtils.AassemblyName(new AssemblyName(text));
                }
            }
            catch
            {
                assembly = null;
            }
            if (assembly == null && text.Split(new char[]
            {
                ','
            })[0].LastIndexOf(".resources", StringComparison.Ordinal) < 0)
            {
                Console.WriteLine("*** Load Assembly Failed: {0}", resolveEventArgs.Name);
            }
            return(assembly);
        }
Exemplo n.º 2
0
        private static AssemblyReferenceInfo GetAssemblyReferenceInfo(
            IDictionary <string, AssemblyReferenceInfo> assemblies,
            AssemblyName assemblyName,
            AppDomain appDomainWithBindingRedirects,
            VisualizerOptions options,
            string fileName = "")
        {
            if (options.SkipSystem && AssemblyInformationProvider.IsSystemAssembly(assemblyName))
            {
                return(null);
            }

            if (!string.IsNullOrEmpty(options.ReferencedStartsWith) &&
                !assemblyName.FullName.StartsWith(options.ReferencedStartsWith, StringComparison.OrdinalIgnoreCase))
            {
                return(null);
            }

            var assemblyFullName = appDomainWithBindingRedirects != null
                ? appDomainWithBindingRedirects.ApplyPolicy(assemblyName.FullName)
                : assemblyName.FullName;

            if (assemblies.TryGetValue(assemblyFullName, out AssemblyReferenceInfo assemblyReferenceInfo))
            {
                return(assemblyReferenceInfo);
            }

            assemblyReferenceInfo = new AssemblyReferenceInfo(assemblyName, new AssemblyName(assemblyFullName), fileName);
            assemblies.Add(assemblyFullName, assemblyReferenceInfo);
            return(assemblyReferenceInfo);
        }
Exemplo n.º 3
0
 private string ApplyPolicy(string name)
 {
     while (true)
     {
         var newName = domain.ApplyPolicy(name);
         if (newName == name)
         {
             return(name);
         }
         name = newName;
     }
 }