gpt4 book ai didi

c# - 将 Microsoft.Build.BuildEngine.Engine 迁移到 Microsoft.Build.Evaluation.ProjectCollection

转载 作者:太空狗 更新时间:2023-10-29 20:25:16 25 4
gpt4 key购买 nike

在 Microsoft 将 BuildEngine.Engine 和 BuildEngine.Project 标记为过时后,我尝试使用 Microsoft 的新提案,您如何在下面看到它。但我不知道在哪里可以集成 xmlprojectfile。这里有人知道这个问题的解决方案吗?

XML 项目文件内容

<Project xmlns="http://schemas.microsoft.com/developer/msbuild/2003">
<ItemGroup>
<ProjectToBuild Include ="myproject.csproj" />
</ItemGroup>
<Target Name="Build">
<MSBuild Projects="@(ProjectToBuild)"
Properties="Configuration=Debug" StopOnFirstFailure="true" />
</Target>
</Project>

旧版本(但已过时)

Microsoft.Build.BuildEngine.Engine engine = new Microsoft.Build.BuildEngine.Engine();
engine.DefaultToolsVersion = "4.0";
engine.RegisterLogger(new ConsoleLogger());
Microsoft.Build.BuildEngine.Project project = new Microsoft.Build.BuildEngine.Project(engine);
project.Load(xmlprojectfile);
if (!project.Build())
{
Console.ReadLine();
}

新的不工作版本

Microsoft.Build.Evaluation.ProjectCollection collection = new Microsoft.Build.Evaluation.ProjectCollection();
collection.DefaultToolsVersion = "4.0";
collection.RegisterLogger(new ConsoleLogger());
Microsoft.Build.Evaluation.Project project = new Microsoft.Build.Evaluation.Project(collection);
if (!project.Build())
{
Console.ReadLine();
}

最佳答案

您还需要创建一个 Microsoft.Build.Execution.BuildRequestData 对象,其中包含 XML 项目文件和 std 属性(如配置和平台)的字典。然后创建一个 Microsoft.Build.Execution.BuildParameters 对象,其中包含代码片段中的 ProjectCollection 对象,并将其传递给默认的 Microsoft.Build.Execution.BuildManager。

powershell伪代码:

#set build properties
$props = new-object "System.Collections.Generic.Dictionary``2[[System.String],[System.String]]"
$props.Add("Configuration",$config)
$props.Add("Platform",$platform)

#create the projectCollection
$projectCollection = new-object Microsoft.Build.Evaluation.ProjectCollection -argumentList $props,$loggers,"ConfigurationFile,Registry"
$request = new-object Microsoft.Build.Execution.BuildRequestData -argumentlist $project,$props,$null,$targets,$null

#create a BuildParameters object to hold the Project Collection
$parameters = new-object Microsoft.Build.Execution.BuildParameters -argumentlist @($projectCollection)
$parameters.MaxNodeCount = 1
$parameters.Loggers = $projectCollection.Loggers
$parameters.ToolsetDefinitionLocations = "ConfigurationFile,Registry"
$parameters.DefaultToolsVersion = $toolsVersion

#get the build manager and submit a build request with the appropriate parameters
$manager = [Microsoft.Build.Execution.BuildManager]::DefaultBuildManager
$result = $manager.Build($parameters, $request)

关于c# - 将 Microsoft.Build.BuildEngine.Engine 迁移到 Microsoft.Build.Evaluation.ProjectCollection,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7846967/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com