/// <summary> /// Load a project content using Reflection in a separate AppDomain. /// This method first tries to load the assembly from the disk cache, if it exists there. /// If it does not exist in disk cache, it creates a new AppDomain, instanciates a ReflectionLoader in it /// and loads the assembly <paramref name="filename"/>. /// If the file does not exist, <paramref name="include"/> is loaded from GAC. /// </summary> protected ReflectionProjectContent ReflectionLoadProjectContent(string filename, string include) { DomPersistence persistence; bool tempPersistence; if (this.persistence == null) { tempPersistence = true; persistence = new DomPersistence(Path.GetTempPath(), this); } else { // non-temp persistence tempPersistence = false; persistence = this.persistence; ReflectionProjectContent pc = persistence.LoadProjectContentByAssemblyName(filename); if (pc != null) { return(pc); } pc = persistence.LoadProjectContentByAssemblyName(include); if (pc != null) { return(pc); } } AppDomainSetup setup = new AppDomainSetup(); setup.DisallowCodeDownload = true; setup.ApplicationBase = AppDomain.CurrentDomain.BaseDirectory; AppDomain domain = AppDomain.CreateDomain("AssemblyLoadingDomain", AppDomain.CurrentDomain.Evidence, setup); string database; try { object o = domain.CreateInstanceAndUnwrap(typeof(ReflectionLoader).Assembly.FullName, typeof(ReflectionLoader).FullName); ReflectionLoader loader = (ReflectionLoader)o; database = loader.LoadAndCreateDatabase(filename, include, persistence.CacheDirectory); } catch (FileLoadException e) { database = null; HostCallback.ShowAssemblyLoadErrorInternal(filename, include, e.Message); } catch (Exception e) { database = null; HostCallback.ShowError("Error loading code-completion information for " + include + " from " + filename, e); throw e; } finally { AppDomain.Unload(domain); } if (database == null) { LoggingService.Debug("AppDomain finished but returned null..."); return(null); } else { LoggingService.Debug("AppDomain finished, loading cache..."); try { return(persistence.LoadProjectContent(database)); } finally { if (tempPersistence) { try { File.Delete(database); } catch {} } } } }
protected virtual IProjectContent LoadProjectContent(string itemInclude, string itemFileName) { string shortName = itemInclude; int pos = shortName.IndexOf(','); if (pos > 0) { shortName = shortName.Substring(0, pos); } Assembly assembly = GetDefaultAssembly(shortName); ReflectionProjectContent pc = null; if (assembly != null) { if (persistence != null) { pc = persistence.LoadProjectContentByAssemblyName(assembly.FullName); } if (pc == null) { pc = new ReflectionProjectContent(assembly, this); if (persistence != null) { persistence.SaveProjectContent(pc); } } if (pc != null) { // add default .NET assemblies to redirected assemblies (both when loaded from persistence // and when loaded using Reflection) lock (redirectedAssemblyNames) { redirectedAssemblyNames[shortName] = pc.AssemblyFullName; } } } else { if (pc == null) { pc = ReflectionLoadProjectContent(itemFileName, itemInclude); } // find real file name for cecil: //if (File.Exists(itemFileName)) { // pc = CecilReader.LoadAssembly(itemFileName, this); //} else { // GacAssemblyName asmName = GacInterop.FindBestMatchingAssemblyName(itemInclude); // if (persistence != null && asmName != null) { // //LoggingService.Debug("Looking up in DOM cache: " + asmName.FullName); // pc = persistence.LoadProjectContentByAssemblyName(asmName.FullName); // } // if (pc == null && asmName != null) { // string subPath = Path.Combine(asmName.Name, GetVersion__Token(asmName)); // subPath = Path.Combine(subPath, asmName.Name + ".dll"); // foreach (string dir in Directory.GetDirectories(GacInterop.GacRootPath, "GAC*")) { // itemFileName = Path.Combine(dir, subPath); // if (File.Exists(itemFileName)) { // pc = CecilReader.LoadAssembly(itemFileName, this); // if (persistence != null) { // persistence.SaveProjectContent(pc); // } // break; // } // } // } // if (pc == null) { // HostCallback.ShowAssemblyLoadErrorInternal("?", itemInclude, "Could not find assembly file."); // } //} } return(pc); }