private void GetReferencedAssemblies(Assembly assembly) { var assemblyNameList = assembly.GetReferencedAssemblies(); foreach (var tempAssembly in assemblyNameList) { Assembly loadedTempAssembly = null; var token = tempAssembly.GetPublicKeyToken(); var keyToken = BitConverter.ToString(token).Replace("-", string.Empty); if (KnownTokens.Contains(keyToken)) { continue; } try { loadedTempAssembly = Assembly.Load(tempAssembly.FullName); } catch (Exception) { try { if (tempAssembly.CodeBase == null) { continue; } var tempUri = new UriBuilder(tempAssembly.CodeBase); var tempPath = tempUri.Uri.AbsolutePath; loadedTempAssembly = Assembly.LoadFrom(tempPath); } catch (Exception) { continue; } } var uri = new UriBuilder(loadedTempAssembly.CodeBase); var assemblyPath = uri.Uri.AbsolutePath; if (!assemblyPath.StartsWith(NetFxFolderPath, StringComparison.OrdinalIgnoreCase) && !GacedAssemblyList.Contains(assemblyPath) && !CheckIfAssemblyPathHasKnownToken(assemblyPath)) { if (!assemblyPath.StartsWith(SitePath, StringComparison.OrdinalIgnoreCase)) { GacedAssemblyList.Add(assemblyPath); } GetReferencedAssemblies(loadedTempAssembly); } } }
public List <string> GetGacedAssemblies() { if (string.IsNullOrEmpty(SitePath) || !Directory.Exists(SitePath)) { return(new List <string>()); } if (RemoteSystemInfos.Servers != null) { return(new List <string>()); } #if DEBUG return(new List <string>()); #endif var tempDomain = AppDomain.CreateDomain("ReflectionForGac"); var fileList = new string[] {}; try { fileList = Directory.GetFiles(SitePath, "*.dll", SearchOption.AllDirectories); } catch (Exception ex) { TraceHelper.Tracer.WriteTrace(ex.ToString()); } if (fileList.Any()) { foreach (var file in fileList) { try { GetReferencedAssemblies(Assembly.LoadFrom(file)); } catch { // failed to load some assembly. Its best effort basis } } } //fileList = Directory.GetFiles(SitePath, "*.aspx", SearchOption.AllDirectories); //foreach (var file in fileList) //{ // Assembly assembly = null; // try // { // //BuildManager.GetReferencedAssemblies() // // This throws because it needs virtual path and we have physical path. // var p = BuildManager.CreateInstanceFromVirtualPath("testsite/default.aspx", typeof(Page)); // } // catch (Exception ex) // { // } //} AppDomain.Unload(tempDomain); return(GacedAssemblyList.ToList()); }