Esempio n. 1
0
        /// <summary>
        /// Adds references to one or more projects.
        /// </summary>
        public async Task AddAsync(params Project[] projects)
        {
            await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync();

            IVsSolution solution = await VS.Services.GetSolutionAsync();

            IVsReferenceManagerUser manager = GetManager();
            Lazy <IVsProjectReferenceProviderContext>       projectContext = new(() => GetProviderContext <IVsProjectReferenceProviderContext>(manager), false);
            Lazy <IVsSharedProjectReferenceProviderContext> sharedContext  = new(() => GetProviderContext <IVsSharedProjectReferenceProviderContext>(manager), false);

            foreach (Project project in projects)
            {
                project.GetItemInfo(out IVsHierarchy hierarchy, out uint itemId, out _);

                IVsSharedAssetsProject?sharedProject = hierarchy.GetSharedAssetsProject();
                if (sharedProject is not null)
                {
                    IVsSharedProjectReference reference = (IVsSharedProjectReference)sharedContext.Value.CreateReference();
                    PopulateSharedProjectReference(reference, solution, project, sharedProject);
                    sharedContext.Value.AddReference(reference);
                }
                else
                {
                    ErrorHandler.ThrowOnFailure(solution.GetUniqueNameOfProject(hierarchy, out string uniqueName));
                    ErrorHandler.ThrowOnFailure(solution.GetGuidOfProject(hierarchy, out Guid projectGuid));

                    IVsProjectReference reference = (IVsProjectReference)projectContext.Value.CreateReference();
                    reference.Name     = project.Name;
                    reference.FullPath = project.FullPath;
                    reference.Identity = projectGuid.ToString("b");
                    // The reference specification is made up of the project’s GUID and the
                    // project's Visual Studio unique name, separated by a "|" character.
                    reference.ReferenceSpecification = $"{projectGuid:b}|{uniqueName}";
                    projectContext.Value.AddReference(reference);
                }
            }

            if (projectContext.IsValueCreated)
            {
                manager.ChangeReferences((uint)__VSREFERENCECHANGEOPERATION.VSREFERENCECHANGEOPERATION_ADD, projectContext.Value);
            }

            if (sharedContext.IsValueCreated)
            {
                manager.ChangeReferences((uint)__VSREFERENCECHANGEOPERATION.VSREFERENCECHANGEOPERATION_ADD, sharedContext.Value);
            }
        }
Esempio n. 2
0
        /// <inheritdoc/>
        public IEnumerator <Reference> GetEnumerator()
        {
            ThreadHelper.ThrowIfNotOnUIThread();

            // Not all projects can have references (for example Shared Projects),
            // so when enumerating over the references in the project, we won't throw
            // an error if the manager or provider context cannot be retrieved.
            if (TryGetManager(out IVsReferenceManagerUser manager))
            {
                IVsSharedProjectReferenceProviderContext?sharedProjectContext = null;

                foreach (IVsReferenceProviderContext context in manager.GetProviderContexts().OfType <IVsReferenceProviderContext>())
                {
                    // Remember the shared project context, because it may not actually provide the
                    // references to shared projects, meaning we may have to create them ourselves.
                    if (context is IVsSharedProjectReferenceProviderContext shared)
                    {
                        sharedProjectContext = shared;
                    }

                    foreach (IVsReference reference in context.References.OfType <IVsReference>())
                    {
                        if (reference is IVsAssemblyReference assemblyReference)
                        {
                            yield return(new AssemblyReference(assemblyReference));
                        }
                        else if (reference is IVsProjectReference projectReference)
                        {
                            yield return(new ProjectReference(projectReference));
                        }
                        else
                        {
                            yield return(new Reference(reference));
                        }
                    }
                }

                // Shared projects don't seem to be listed in the provider contexts, so if there is a context
                // for shared projects but it's empty, then we'll define the shared project references ourselves.
                if (sharedProjectContext is not null && sharedProjectContext.References.Length == 0)
                {
                    IVsSolution?solution = null;

                    _project.GetItemInfo(out IVsHierarchy hierarchy, out _, out _);

                    foreach (IVsHierarchy sharedHierarchy in hierarchy.EnumOwningProjectsOfSharedAssets())
                    {
                        // A shared project seems to list itself as an owning project, so ignore
                        // this hierarchy if it's the same one that we got from our project.
                        if (sharedHierarchy == hierarchy)
                        {
                            continue;
                        }

                        IVsSharedAssetsProject?sharedProject = sharedHierarchy.GetSharedAssetsProject();
                        if (sharedProject is not null)
                        {
                            Project?project = SolutionItem.FromHierarchy(sharedHierarchy, VSConstants.VSITEMID_ROOT) as Project;
                            if (project is not null)
                            {
                                if (solution is null)
                                {
                                    solution = VS.GetRequiredService <SVsSolution, IVsSolution>();
                                }

                                IVsSharedProjectReference reference = (IVsSharedProjectReference)sharedProjectContext.CreateReference();
                                PopulateSharedProjectReference(reference, solution, project, sharedProject);
                                yield return(new ProjectReference(reference));
                            }
                        }
                    }
                }
            }
        }