Exemple #1
0
        public IEnumerable<string> FindReferringAssemblies(Assembly testAssembly, string directory, bool recursive)
        {
            List<string> referringAssemblies = new List<string>();
            List<string> binaries = new List<string>();
            try
            {
                ReferringAssemblyStatusChanged(this,
                                               new ReferringAssemblyStatusChangeEventArgs
                                                   {StatusText = "Finding all binaries"});
                FindAssemblies(new DirectoryInfo(directory), binaries, recursive);
            }
            catch (Exception ex)
            {
                UpdateProgress(Resource.FailedToListBinaries, -2);
                return null;
            }

            if (binaries.Count == 0) return referringAssemblies;
            int baseDirPathLength = directory.Length;
            if (!directory.EndsWith("\\")) baseDirPathLength++;

            int i =0;
            foreach (var binary in binaries)
            {
                string message = String.Format(Resource.AnalyzingAssembly, Path.GetFileName(binary));
                int progress = (i++ * 100) / binaries.Count;
                if (progress == 100) progress = 99;
                if (!UpdateProgress(message, progress)) return referringAssemblies;

                try
                {
                    Assembly assembly = Assembly.LoadFile(binary);
                    DependencyWalker dw = new DependencyWalker();
                    List<string> loadErrors;
                    var dependencies = dw.FindDependencies(assembly, false, out loadErrors);
                    if (null == dependencies) continue;
                    if (dependencies.Where(p => String.Compare(p.FullName, testAssembly.FullName, StringComparison.OrdinalIgnoreCase) == 0).Count() > 0)
                    {
                        referringAssemblies.Add(binary.Remove(0, baseDirPathLength));
                    }
                    errors.AddRange(loadErrors);
                }
                catch (ArgumentException) { }
                catch (FileLoadException) { }
                catch (FileNotFoundException) { }
                catch (BadImageFormatException) { }
            }

            return referringAssemblies.OrderBy(p =>p);
        }
        public IEnumerable <string> FindReferringAssemblies(Assembly testAssembly, string directory, bool recursive)
        {
            List <string> referringAssemblies = new List <string>();
            List <string> binaries            = new List <string>();

            try
            {
                ReferringAssemblyStatusChanged(this,
                                               new ReferringAssemblyStatusChangeEventArgs
                {
                    StatusText = "Finding all binaries"
                });
                FindAssemblies(new DirectoryInfo(directory), binaries, recursive);
            }
            catch (Exception ex)
            {
                UpdateProgress(Resource.FailedToListBinaries, -2);
                return(null);
            }

            if (binaries.Count == 0)
            {
                return(referringAssemblies);
            }
            int baseDirPathLength = directory.Length;

            if (!directory.EndsWith("\\"))
            {
                baseDirPathLength++;
            }

            int i = 0;

            foreach (var binary in binaries)
            {
                string message  = String.Format(Resource.AnalyzingAssembly, Path.GetFileName(binary));
                int    progress = (i++ *100) / binaries.Count;
                if (progress == 100)
                {
                    progress = 99;
                }
                if (!UpdateProgress(message, progress))
                {
                    return(referringAssemblies);
                }

                try
                {
                    Assembly         assembly = Assembly.LoadFile(binary);
                    DependencyWalker dw       = new DependencyWalker();
                    List <string>    loadErrors;
                    var dependencies = dw.FindDependencies(assembly, false, out loadErrors);
                    if (null == dependencies)
                    {
                        continue;
                    }
                    if (dependencies.Where(p => String.Compare(p.FullName, testAssembly.FullName, StringComparison.OrdinalIgnoreCase) == 0).Count() > 0)
                    {
                        referringAssemblies.Add(binary.Remove(0, baseDirPathLength));
                    }
                    errors.AddRange(loadErrors);
                }
                catch (ArgumentException) { }
                catch (FileLoadException) { }
                catch (FileNotFoundException) { }
                catch (BadImageFormatException) { }
            }

            return(referringAssemblies.OrderBy(p => p));
        }