简体   繁体   中英

How do I get the projects to build in the active Visual Studio configuration?

I am working on a small Visual Studio extension that acts on projects in a solution based on if they are set to build in the active build configuration or not. The problem I am having is that I cannot figure out how to determine what those projects are.

I have implemented IVsUpdateSolutionEvents , in which I implement OnActiveProjectCfgChange . I can get Visual Studio to enter the code block when I change configurations, and I have been able to get it to do many of the things that I would like to do, but without being able to determine what projects should be built in the active configuration, I am dead in the water.

My implementation so far is:

public int OnActiveProjectCfgChange(IVsHierarchy pIVsHierarchy)
{

    var activeProjects = new HashSet<string>(); // TODO: Get projects in active configuration
    foreach (Project project in _dte.Solution.Projects)
    {
        if (project.Kind != "{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}" // C#
            && project.Kind != "{F184B08F-C81C-45F6-A57F-5ABD9991F28F}" // VB
            && project.Kind != "{13B7A3EE-4614-11D3-9BC7-00C04F79DE25}" // VSA
           )
            continue;

        IVsHierarchy projectHierarchy;
        _solutionService.GetProjectOfUniqueName(project.UniqueName, out projectHierarchy);
        if (activeProjects.Contains(project.UniqueName))
        {
            // Project is to be built
        }
        else
        {
            // Project is not to be built
        }

        return VSConstants.S_OK;
    }
}

What I need to do is figure out how to fill in the HashSet at the beginning of the function. (Marked with TODO ). I have searched and searched, but I have not found what I need.

Does anybody have any references to documentation or sample code that might help me move forward?

You can use SolutionContext.ShouldBuild property

 foreach (SolutionContext solutionContext in _applicationObject.Solution.SolutionBuild.ActiveConfiguration.SolutionContexts)
 {
     if (solutionContext.ShouldBuild)
      activeProjects.Add(solutionContext.ProjectName);
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM