Пример #1
0
        /// <summary>
        /// Attempts to load the specified project if it hasn't already been loaded.
        /// </summary>
        /// <param name="path">The path to the project to load.</param>
        /// <param name="toolsVersion">The ToolsVersion to use when loading the project.</param>
        /// <param name="projectCollection">The <see cref="ProjectCollection"/> to load the project into.</param>
        /// <param name="globalProperties">The <see cref="IDictionary{String,String}" /> to use when evaluating the project.</param>
        /// <param name="project">Contains the loaded <see cref="Project"/> if one was loaded.</param>
        /// <returns><code>true</code> if the project was loaded, otherwise <code>false</code>.</returns>
        private bool TryLoadProject(string path, string toolsVersion, ProjectCollection projectCollection, IDictionary <string, string> globalProperties, out Project project)
        {
            project = null;

            bool shouldLoadProject;

            string fullPath = path.ToFullPathInCorrectCase();

            lock (_loadedProjects)
            {
                shouldLoadProject = _loadedProjects.Add(fullPath);
            }

            if (!shouldLoadProject)
            {
                return(false);
            }

            long now = DateTime.Now.Ticks;

            try
            {
                project = new Project(fullPath, globalProperties, toolsVersion, projectCollection, DefaultProjectLoadSettings);

                ProjectLoaderFactory.LogProjectStartedEvent(_logger, project.CreateProjectInstance(ProjectInstanceSettings.ImmutableWithFastItemLookup, ProjectLoaderFactory.SharedEvaluationContext));
            }
            catch (InvalidProjectFileException e)
            {
                _logger.LogError(e.Message, e.ErrorCode, e.ProjectFile, e.LineNumber, e.ColumnNumber);

                return(false);
            }
            catch (Exception e)
            {
                _logger.LogError(message: e.ToString());

                return(false);
            }

            if (CollectStats)
            {
                Statistics.TryAddProjectLoadTime(path, TimeSpan.FromTicks(DateTime.Now.Ticks - now));
            }

            return(true);
        }
Пример #2
0
        private ProjectInstance CreateProjectInstance(string projectFullPath, IDictionary <string, string> globalProperties, ProjectCollection projectCollection)
        {
            ProjectInstance projectInstance = Project.FromFile(
                projectFullPath,
                new ProjectOptions
            {
                EvaluationContext = ProjectLoaderFactory.SharedEvaluationContext,
                GlobalProperties  = globalProperties,
                LoadSettings      = DefaultProjectLoadSettings,
                ProjectCollection = projectCollection,
            })
                                              .CreateProjectInstance(
                ProjectInstanceSettings.ImmutableWithFastItemLookup,
                ProjectLoaderFactory.SharedEvaluationContext);

            ProjectLoaderFactory.LogProjectStartedEvent(_logger, projectInstance);

            return(projectInstance);
        }