C# 使用API时从MSBUILD跳过/排除项目类型

C# 使用API时从MSBUILD跳过/排除项目类型,c#,msbuild,msbuild-api,C#,Msbuild,Msbuild Api,我正在使用MSBUILD API使用服务构建解决方案 乙二醇 var pc=newprojectcollection(); var buildProperties=新字典 { {“配置”、“发布”}, {“平台”,“任何CPU”}, {“OutputPath”,_OutputPath} }; var buildParameters=新的buildParameters(pc); var buildRequest=newbuildrequestdata(_buildFile,buildPropert

我正在使用MSBUILD API使用服务构建解决方案

乙二醇

var pc=newprojectcollection();
var buildProperties=新字典
{
{“配置”、“发布”},
{“平台”,“任何CPU”},
{“OutputPath”,_OutputPath}
};
var buildParameters=新的buildParameters(pc);
var buildRequest=newbuildrequestdata(_buildFile,buildProperties,null,new[]{“Clean”,“Rebuild”},null);
var buildResult=BuildManager.DefaultBuildManager.Build(buildParameters,buildRequest);
我希望能够做的是传入一个排除的项目类型或扩展的列表。首先,我想排除:

  • 数据库项目
  • WinRT项目
  • 通用MSBUILD文件(无项目类型GUID)
有没有办法通过将一些参数传递给MSBUILD管理器来解决此问题?

这不是您想要的,但我只是碰巧在旧的工作中处理了MSBUILD API,并认为以下代码可能很有用:

var basePath = "path-to-where-source-is";
var outputDir = "path-to-output";

// Setup some properties that'll apply to all projs
var pc = Microsoft.Build.Evaluation.ProjectCollection.GlobalProjectCollection;
pc.SetGlobalProperty("Configuration", "Debug");
pc.SetGlobalProperty("Platform", "Any CPU");
pc.SetGlobalProperty("OutDir", outputDir);

// Generate the metaproject that represents a given solution file
var slnProjText = SolutionWrapperProject.Generate(
    Path.Combine(basePath, "NAME-OF-SOLUTION-FILE.sln"), 
    "4.0", 
    null);

// It's now a nice (well, ugly) XML blob, so read it in
using(var srdr = new StringReader(slnProjText))
using(var xrdr = XmlReader.Create(srdr))
{
    // Load the meta-project into the project collection        
    var slnProj = pc.LoadProject(xrdr, "4.0");

    // Slice and dice the projects in solution with LINQ to
    // get a nice subset to work with
    var solutionProjects = 
        from buildLevel in Enumerable.Range(0, 10)
        let buildLevelType = "BuildLevel" + buildLevel
        let buildLevelItems = slnProj.GetItems(buildLevelType)
        from buildLevelItem in buildLevelItems
        let include = buildLevelItem.EvaluatedInclude
        where !include.Contains("Some thing I don't want to build")
        select new 
        { 
            Include=include, 
            Project = pc.LoadProject(Path.Combine(basePath, include))
        };

    // For each of them, build em!
    foreach (var projectPair in solutionProjects)
    {
        var project = projectPair.Project;
        var include = projectPair.Include;
        var outputPath = outputDir;
        project.SetProperty("OutputPath", outputPath);
        Console.WriteLine("Building project:" + project.DirectoryPath);
        var buildOk = project.Build("Build");
        if(buildOk)
        {
            Console.WriteLine("Project build success!");
        } 
        else
        {
            throw new Exception("Build failed");
        }
    }
}

Yessir-解决方案包装器处理从解决方案语法(这是一个遗留的混乱)到msbuild项目语法的转换,后者更好(和正确的xml)
var basePath = "path-to-where-source-is";
var outputDir = "path-to-output";

// Setup some properties that'll apply to all projs
var pc = Microsoft.Build.Evaluation.ProjectCollection.GlobalProjectCollection;
pc.SetGlobalProperty("Configuration", "Debug");
pc.SetGlobalProperty("Platform", "Any CPU");
pc.SetGlobalProperty("OutDir", outputDir);

// Generate the metaproject that represents a given solution file
var slnProjText = SolutionWrapperProject.Generate(
    Path.Combine(basePath, "NAME-OF-SOLUTION-FILE.sln"), 
    "4.0", 
    null);

// It's now a nice (well, ugly) XML blob, so read it in
using(var srdr = new StringReader(slnProjText))
using(var xrdr = XmlReader.Create(srdr))
{
    // Load the meta-project into the project collection        
    var slnProj = pc.LoadProject(xrdr, "4.0");

    // Slice and dice the projects in solution with LINQ to
    // get a nice subset to work with
    var solutionProjects = 
        from buildLevel in Enumerable.Range(0, 10)
        let buildLevelType = "BuildLevel" + buildLevel
        let buildLevelItems = slnProj.GetItems(buildLevelType)
        from buildLevelItem in buildLevelItems
        let include = buildLevelItem.EvaluatedInclude
        where !include.Contains("Some thing I don't want to build")
        select new 
        { 
            Include=include, 
            Project = pc.LoadProject(Path.Combine(basePath, include))
        };

    // For each of them, build em!
    foreach (var projectPair in solutionProjects)
    {
        var project = projectPair.Project;
        var include = projectPair.Include;
        var outputPath = outputDir;
        project.SetProperty("OutputPath", outputPath);
        Console.WriteLine("Building project:" + project.DirectoryPath);
        var buildOk = project.Build("Build");
        if(buildOk)
        {
            Console.WriteLine("Project build success!");
        } 
        else
        {
            throw new Exception("Build failed");
        }
    }
}