Beispiel #1
0
        /// <summary>
        /// Recupera o caminho do redirecionamento.
        /// </summary>
        /// <param name="assemblyName"></param>
        /// <param name="configFile"></param>
        /// <returns></returns>
        public static PathRedirection GetPathRedirections(string assemblyName, string configFile)
        {
            if (!System.IO.File.Exists(configFile))
            {
                return new PathRedirection {
                           AssemblyName = assemblyName
                }
            }
            ;
            var redirect = new PathRedirection {
                AssemblyName = assemblyName
            };
            var config = new System.Xml.XmlDocument();

            config.Load(configFile);
            var nsmgr = new System.Xml.XmlNamespaceManager(config.NameTable);

            nsmgr.AddNamespace("x", "urn:schemas-microsoft-com:asm.v1");
            var probingNode = config.CreateNavigator().SelectSingleNode("/configuration/runtime/x:assemblyBinding/x:probing", nsmgr);

            if (probingNode != null)
            {
                string privatePath = probingNode.GetAttribute("privatePath", string.Empty);

                if (string.IsNullOrEmpty(privatePath))
                {
                    return(redirect);
                }
                foreach (string p in privatePath.Split(new char[] {
                    ';'
                }))
                {
                    redirect.Directories.Add(System.IO.Path.GetFullPath(p));
                }
            }
            return(redirect);
        }
        /// <summary>
        /// Analiza o assembly informado.
        /// </summary>
        /// <param name="assemblyName"></param>
        /// <param name="throwWhenMissing"></param>
        /// <returns></returns>
        public AsmData AnalyzeRootAssembly(string assemblyName, bool throwWhenMissing)
        {
            _cache = new Dictionary <string, AsmData>();
            _circularDependencyWarningShown = false;
            _parentsStack = new Stack <string>();
            _workingDir   = Environment.CurrentDirectory;
            string  fullPath = System.IO.Path.GetFullPath(assemblyName);
            AsmData ret      = new AsmData(assemblyName, fullPath);
            var     domain   = AppDomain.CurrentDomain;

            try
            {
                if (!System.IO.File.Exists(assemblyName))
                {
                    ret.Path             = "";
                    ret.Validity         = AsmData.AsmValidity.Invalid;
                    ret.AssemblyFullName = "";
                    return(ret);
                }
                System.Reflection.Assembly asm = null;
                try
                {
                    using (var stream = System.IO.File.OpenRead(fullPath))
                    {
                        var raw = new byte[stream.Length];
                        stream.Read(raw, 0, raw.Length);
                        try
                        {
                            asm          = domain.Load(raw);
                            ret.Validity = AsmData.AsmValidity.Valid;
                        }
                        catch (Exception)
                        {
                            asm          = System.Reflection.Assembly.ReflectionOnlyLoad(raw);
                            ret.Validity = AsmData.AsmValidity.ReferencesOnly;
                        }
                    }
                }
                catch (Exception)
                {
                    asm          = null;
                    ret.Validity = AsmData.AsmValidity.Invalid;
                }
                if (asm != null)
                {
                    ret.AssemblyFullName       = asm.FullName;
                    ret.Path                   = fullPath;
                    ret.Architecture           = asm.GetName().ProcessorArchitecture;
                    _currentLoadedArchitecture = ret.Architecture;
                    string tempName = asm.GetName().Name;
                    if (!this.assemblyNameToPathMap.ContainsKey(tempName))
                    {
                        this.assemblyNameToPathMap.Add(tempName, asm.Location);
                    }
                    string cfgFilePath = Redirection.FindConfigFile(ret.Path);
                    if (!string.IsNullOrEmpty(cfgFilePath) && !_allVersionRedirections.ContainsKey(fullPath))
                    {
                        var             versionRedirections = Redirection.GetVersionRedirections(cfgFilePath);
                        PathRedirection pathRedirections    = Redirection.GetPathRedirections(ret.AssemblyFullName, cfgFilePath);
                        _allVersionRedirections.Add(fullPath, versionRedirections);
                        _probingPaths.Add(ret.AssemblyFullName, pathRedirections);
                    }
                    var references = asm.GetReferencedAssemblies().ToList();
                    var fileName   = System.IO.Path.Combine(System.IO.Path.GetDirectoryName(fullPath), string.Format("{0}.XmlSerializers{1}", System.IO.Path.GetFileNameWithoutExtension(fullPath), System.IO.Path.GetExtension(fullPath)));
                    if (System.IO.File.Exists(fileName))
                    {
                        references.Add(System.Reflection.AssemblyName.GetAssemblyName(fileName));
                    }
                    _totalReferencedAssemblies = references.Count;
                    _parentsStack.Push(ret.AssemblyFullName);
                    foreach (System.Reflection.AssemblyName asmName in references)
                    {
                        this.AnalyzeAssembly(asmName, ret, domain, throwWhenMissing);
                        _assembliesFinished++;
                        if (_bgWorker != null)
                        {
                            _bgWorker.ReportProgress((100 * _assembliesFinished) / _totalReferencedAssemblies);
                        }
                    }
                    _parentsStack.Pop();
                }
            }
            finally
            {
            }
            return(ret);
        }