【问题标题】:How to list project based on a configuration from the SolutionFile class如何根据 SolutionFile 类的配置列出项目
【发布时间】:2018-04-26 21:25:26
【问题描述】:

如何从 SolutionFile 类列出特定配置的所有项目?

【问题讨论】:

  • 这就是你想要的?

标签: msbuild solution


【解决方案1】:

我能够得到我想要的结果:

(下面代码的 sn-p 是从 ViewModel 中提取的)

using Microsoft.Build.Construction;
using System.Collections.ObjectModel;

// Step #1 - Instantiate SolutionFile from a solution file name

    vm.SolutionFile = SolutionFile.Parse("SampleSolution.sln");

// Step #2 - Get List of Solution Configurations

    var list = new ObservableCollection<string>();

    foreach (var solutionConfiguration in vm.SolutionFile.SolutionConfigurations)
    {
        list.Add(solutionConfiguration.ConfigurationName + "|" + solutionConfiguration.PlatformName);
    }

    vm.SolutionConfigurations = list;

// Step #3 - Select the desired Configuration 

    vm.Configuration = [select a configuration from vm.SolutionConfigurations]

// Step #4 Get List of Projects based on Configuration

    var list = new ObservableCollection<string>();

    foreach (var project in vm.SolutionFile.ProjectsByGuid)
    {
        foreach (var projectConfiguration in project.Value.ProjectConfigurations)
        {
            if (vm.Configuration.Contains(projectConfiguration.Key.ToString()))
            {
                if (projectConfiguration.Value.IncludeInBuild)
                {
                    list.Add(project.Value.ProjectName);
                    break;
                }
            }
        }
    }

    vm.ProjectsInConfiguration = list;

【讨论】:

  • 这非常有用。这些 API 的文档都是垃圾,从这个 sn-p 中可以学到的技巧是 ProjectInSolution 类的 ProjectConfigurations 由 SOLUTION CONFIGURATION 键控,值是使用哪个项目配置通过该解决方案配置。这是您在查看这些 API 时可能不会轻易注意到的必要部分。谢谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2016-11-20
  • 1970-01-01
  • 1970-01-01
  • 2011-10-15
  • 2013-04-08
  • 2022-11-19
  • 2020-10-17
相关资源
最近更新 更多