Пример #1
0
        /// <summary>
        /// Get export
        /// </summary>
        internal static T GetExport <T>(IVsHierarchy hier)
        {
            System.IServiceProvider sp       = new Shell.ServiceProvider((OLE.Interop.IServiceProvider)hier.GetDTEProject().DTE);
            IComponentModel         compMode = sp.GetService <IComponentModel, SComponentModel>();

            return(compMode.DefaultExportProvider.GetExport <T>().Value);
        }
        private async Task <IProjectItemDesignerTypeUpdateService?> GetUpdateServiceIfCpsProjectAsync(
            ProjectId projectId, CancellationToken cancellationToken)
        {
            if (!_cpsProjects.TryGetValue(projectId, out var updateService))
            {
                await _threadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(alwaysYield : true, cancellationToken);

                cancellationToken.ThrowIfCancellationRequested();
                this.AssertIsForeground();

                updateService = ComputeUpdateService();
                _cpsProjects.TryAdd(projectId, updateService);
            }

            return(updateService);

            IProjectItemDesignerTypeUpdateService?ComputeUpdateService()
            {
                if (!_workspace.IsCPSProject(projectId))
                {
                    return(null);
                }

                var vsProject = (IVsProject?)_workspace.GetHierarchy(projectId);

                if (vsProject == null)
                {
                    return(null);
                }

                if (ErrorHandler.Failed(vsProject.GetItemContext((uint)VSConstants.VSITEMID.Root, out var projectServiceProvider)))
                {
                    return(null);
                }

                var serviceProvider = new Shell.ServiceProvider(projectServiceProvider);

                return(serviceProvider.GetService(typeof(IProjectItemDesignerTypeUpdateService)) as IProjectItemDesignerTypeUpdateService);
            }
        }
        private async Task <IProjectItemDesignerTypeUpdateService> GetUpdateServiceIfCpsProjectAsync(Project project, CancellationToken cancellationToken)
        {
            if (_cpsProjects.TryGetValue(project.Id, out var value))
            {
                return(value);
            }

            var vsWorkspace = project.Solution.Workspace as VisualStudioWorkspaceImpl;

            await ThreadingContext.JoinableTaskFactory.SwitchToMainThreadAsync(alwaysYield : true, cancellationToken);

            cancellationToken.ThrowIfCancellationRequested();

            if (!vsWorkspace.IsCPSProject(project))
            {
                _cpsProjects.TryAdd(project.Id, null);
                return(null);
            }

            var vsProject = (IVsProject)vsWorkspace.GetHierarchy(project.Id);

            if (ErrorHandler.Failed(vsProject.GetItemContext((uint)VSConstants.VSITEMID.Root, out var projectServiceProvider)))
            {
                _cpsProjects.TryAdd(project.Id, null);
                return(null);
            }

            var serviceProvider = new Shell.ServiceProvider(projectServiceProvider);
            var updateService   = serviceProvider.GetService(typeof(IProjectItemDesignerTypeUpdateService)) as IProjectItemDesignerTypeUpdateService;

            if (updateService == null)
            {
                _cpsProjects.TryAdd(project.Id, null);
                return(null);
            }

            _cpsProjects[project.Id] = updateService;
            return(updateService);
        }