Ejemplo n.º 1
0
        private static void MapAssemblyReferences(
            DependencyAnalyzerResult result,
            AppDomain appDomainWithBindingRedirects,
            VisualizerOptions options)
        {
            var copyOfAssemblies = new AssemblyReferenceInfo[result.Assemblies.Count];

            result.Assemblies.Values.CopyTo(copyOfAssemblies, 0);
            foreach (var assembly in copyOfAssemblies)
            {
                foreach (var referencedAssembly in assembly.ReflectionOnlyAssembly.GetReferencedAssemblies())
                {
                    var referencedAssemblyReferenceInfo = GetAssemblyReferenceInfo(
                        result.Assemblies,
                        referencedAssembly,
                        appDomainWithBindingRedirects,
                        options);

                    if (referencedAssemblyReferenceInfo != null)
                    {
                        assembly.AddReference(referencedAssemblyReferenceInfo);
                        referencedAssemblyReferenceInfo.AddReferencedBy(assembly);
                    }
                }
            }
        }
Ejemplo 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);
        }
Ejemplo n.º 3
0
        private static void ResolveFileReferences(
            DependencyAnalyzerResult result,
            ILogger logger,
            AppDomain appDomainWithBindingRedirects,
            VisualizerOptions options)
        {
            foreach (var fileInfo in result.AnalyzedFiles.Where(x => x.IsAssembly()).OrderBy(asm => asm.Name))
            {
                Assembly assembly;
                try
                {
                    assembly = Assembly.ReflectionOnlyLoadFrom(fileInfo.FullName);
                    logger.LogMessage($"File {fileInfo.Name} => {assembly.GetName().Name} {assembly.GetName().Version.ToString()}");
                }
                catch (Exception ex)
                {
                    logger.LogError($"Failed to load assembly '{fileInfo.FullName}': {ex.Message}");
                    continue;
                }

                var assemblyReferenceInfo = GetAssemblyReferenceInfo(
                    result.Assemblies,
                    assembly.GetName(),
                    appDomainWithBindingRedirects,
                    options,
                    fileInfo.Name);

                if (assemblyReferenceInfo != null)
                {
                    assemblyReferenceInfo.ReflectionOnlyAssembly = assembly;
                    assemblyReferenceInfo.AssemblySource         = AssemblySource.Local;
                }
            }
        }
Ejemplo n.º 4
0
        public static DependencyAnalyzerResult Analyze(
            IEnumerable <FileInfo> files,
            AppDomain appDomainWithBindingRedirects,
            ILogger logger,
            VisualizerOptions options,
            string rootFileName = "")
        {
            if (files == null)
            {
                throw new ArgumentNullException(nameof(files));
            }
            if (logger == null)
            {
                throw new ArgumentNullException(nameof(logger));
            }
            if (options == null)
            {
                throw new ArgumentNullException(nameof(options));
            }
            if (rootFileName == null)
            {
                throw new ArgumentNullException(nameof(rootFileName));
            }

            var result = new DependencyAnalyzerResult(files.ToArray());

            if (result.AnalyzedFiles.Count <= 0)
            {
                return(result);
            }

            ResolveFileReferences(result, logger, appDomainWithBindingRedirects, options);
            MapAssemblyReferences(result, appDomainWithBindingRedirects, options);
            ResolveNonFileReferences(result, logger);
            FindRootAssemblies(result, logger, rootFileName);
            MarkReferencedAssemblies(result);

            return(result);
        }