将 Microsoft.Build.BuildEngine.Engine 迁移到 Microsoft.Build.Evaluation.ProjectCollection

发布于 2024-12-10 21:17:15 字数 1376 浏览 0 评论 0原文

在 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();
}

after Microsoft marked the BuildEngine.Engine and BuildEngine.Project as obsolete i have tried to use the new proposal from Microsoft how you can see it underneath. But i have no idea where i can integrate the xmlprojectfile. Is here someone who knows the solution of this problem?

The XML project file content

<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>

The old and working version (but obsolete)

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();
}

The new not working verison

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();
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

孤云独去闲 2024-12-17 21:17:15

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

You also need to create a Microsoft.Build.Execution.BuildRequestData object containing the XML project file and a dictionary of std properties like Configuration and Platform. Then you create a Microsoft.Build.Execution.BuildParameters object containing your ProjectCollection object from your code snippet and pass that off to the default Microsoft.Build.Execution.BuildManager.

powershell pseudo code:

#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)
请叫√我孤独 2024-12-17 21:17:15

您缺少project.Load(xmlprojectfile);迁移代码中的行。我猜你必须以某种方式将 xmlprojectfile 添加到projectcollection 中。

You are missing project.Load(xmlprojectfile); line in your migrated code. You have to add xmlprojectfile into projectcollection somehow, I guess.

记忆消瘦 2024-12-17 21:17:15

不要使用 new 关键字创建 new 项目:

// var project = new Project(collection);

使用以下命令:

 var project = collection.LoadProject("myproject.csproj")

Instead of creating a new Project with new keyword:

// var project = new Project(collection);

Use this:

 var project = collection.LoadProject("myproject.csproj")
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文