/// <summary> /// Find a project in the current solution /// </summary> /// <param name="projectName">Name of the project to find. The name is compared case insensitive</param> /// <returns>a project object or null when no match is found.</returns> public async Task <Project?> FindProjectsAsync(string projectName) { await ThreadHelper.JoinableTaskFactory.SwitchToMainThreadAsync(); IVsSolution solution = await VS.Services.GetSolutionAsync(); IEnumerable <IVsHierarchy> hierarchies = solution.GetAllProjectHierarchies(ProjectStateFilter.All); foreach (IVsHierarchy hierarchy in hierarchies) { Project?proj = await SolutionItem.FromHierarchyAsync(hierarchy, VSConstants.VSITEMID_ROOT) as Project; if (proj != null) { if (proj.Type == SolutionItemType.Project && string.Compare(proj.Name, projectName, true) == 0) { return(proj); } } } return(null); }
/// <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)); } } } } } }