using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Reflection; namespace Umbraco.Core { internal static class AssemblyExtensions { /// /// Returns the file used to load the assembly /// /// /// public static FileInfo GetAssemblyFile(this Assembly assembly) { var codeBase = assembly.CodeBase; var uri = new Uri(codeBase); var path = uri.LocalPath; return new FileInfo(path); } /// /// Returns true if the assembly is the App_Code assembly /// /// /// public static bool IsAppCodeAssembly(this Assembly assembly) { if (assembly.FullName.StartsWith("App_Code")) { try { Assembly.Load("App_Code"); return true; } catch (FileNotFoundException) { //this will occur if it cannot load the assembly return false; } } return false; } /// /// Returns true if the assembly is the compiled global asax. /// /// /// public static bool IsGlobalAsaxAssembly(this Assembly assembly) { //only way I can figure out how to test is by the name return assembly.FullName.StartsWith("App_global.asax"); } /// /// Returns the file used to load the assembly /// /// /// public static FileInfo GetAssemblyFile(this AssemblyName assemblyName) { var codeBase = assemblyName.CodeBase; var uri = new Uri(codeBase); var path = uri.LocalPath; return new FileInfo(path); } /// /// Gets the objects for all the assemblies recursively referenced by a specified assembly. /// /// The assembly. /// The objects for all the assemblies recursively referenced by the specified assembly. public static IEnumerable GetDeepReferencedAssemblies(this Assembly assembly) { var allAssemblies = TypeFinder.GetAllAssemblies(); var visiting = new Stack(); var visited = new HashSet(); visiting.Push(assembly); visited.Add(assembly); while (visiting.Count > 0) { var visAsm = visiting.Pop(); foreach (var refAsm in visAsm.GetReferencedAssemblies() .Select(refAsmName => allAssemblies.SingleOrDefault(x => string.Equals(x.GetName().Name, refAsmName.Name, StringComparison.Ordinal))) .Where(x => x != null && visited.Contains(x) == false)) { yield return refAsm.GetName(); visiting.Push(refAsm); visited.Add(refAsm); } } } } }