예제 #1
0
        public virtual IProjectContent GetProjectContentForReference(string itemInclude, string itemFileName)
        {
            lock (_contents)
            {
                IProjectContent pc = GetExistingProjectContent(itemInclude, itemFileName);
                if (pc != null)
                {
                    return(pc);
                }

                LoggingService.Debug("Loading PC for " + itemInclude);

                string shortName = itemInclude;
                int    pos       = shortName.IndexOf(',');
                if (pos > 0)
                {
                    shortName = shortName.Substring(0, pos);
                }

                HostCallback.BeginAssemblyLoad(shortName);
#if DEBUG
                int time = Environment.TickCount;
#endif

                try
                {
                    pc = LoadProjectContent(itemInclude, itemFileName);
                }
                catch (Exception ex)
                {
                    HostCallback.ShowAssemblyLoadErrorInternal(itemFileName, itemInclude, "Error loading assembly:\n" + ex.ToString());
                    throw ex;
                }
                finally
                {
#if DEBUG
                    LoggingService.Debug(string.Format("Loaded {0} in {1}ms", itemInclude, Environment.TickCount - time));
#endif
                    HostCallback.FinishAssemblyLoad();
                }

                if (pc != null)
                {
                    ReflectionProjectContent reflectionProjectContent = pc as ReflectionProjectContent;
                    if (reflectionProjectContent != null && reflectionProjectContent.AssemblyFullName != null)
                    {
                        _contents[reflectionProjectContent.AssemblyFullName] = pc;
                    }
                    _contents[itemInclude]  = pc;
                    _contents[itemFileName] = pc;
                }
                return(pc);
            }
        }
예제 #2
0
        private void Init(string fileName)
        {
            ParseInformation parseInfo = HostCallback.GetParseInformation(fileName);

            if (parseInfo != null)
            {
                _cu = parseInfo.MostRecentCompilationUnit;
            }

            if (_cu != null)
            {
                _callingClass   = _cu.GetInnermostClass(_caretLine, _caretColumn);
                _projectContent = _cu.ProjectContent;
            }
            else
            {
                _projectContent = HostCallback.GetCurrentProjectContent();
            }
            if (_projectContent == null)
            {
                throw new ArgumentException("projectContent not found!");
            }
        }
        /// <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 {}
                    }
                }
            }
        }