如何在自定义任务中访问当前项目上下文?

时间:2022-01-25 07:23:21

How do I access current Project context within a custom Task in MSBuild? At first it appeared as though the GlobalProjectCollection reference on ProjectCollection would allow access, but this appears to be empty, at least when running MSBuild from the commandline.

如何在MSBuild中的自定义任务中访问当前项目上下文?起初,好像ProjectCollection上的GlobalProjectCollection引用允许访问,但这似乎是空的,至少在从命令行运行MSBuild时是空的。

I can currently get new Project instance based off of the current project file in the following manner:

我目前可以从当前的项目文件中获取新的项目实例,方式如下:

List projectAssemblies = new List();
using (XmlReader projectFileReader =
    XmlReader.Create(this.BuildEngine.ProjectFileOfTaskNode))
{
    Project project = new Project(projectFileReader);
    foreach (ProjectItem item in project.AllEvaluatedItems)
    {
      ... woo hoo ...
    }
}

but it just seems like a lot of trouble. How can I just get access to the project from which my task is invoked?

但这似乎有很多麻烦。如何访问调用我的任务的项目?

1 个解决方案

#1


5  

You can not. MSBuild was specifically designed so that individual tasks only have access to the parameters that you explicitly passed to the task, and nothing else. That makes MSBuild files easier to read, now that you know that each task is only affected by what your specidied when you called this task.

你不能。MSBuild是专门设计的,以便每个任务只能访问显式传递给任务的参数,而不能访问其他参数。这使得MSBuild文件更易于阅读,因为您知道每个任务只受调用此任务时指定的内容的影响。

The code you have is not creating a copy of the project, it is creating a new instance, so you should not expect properties to be the same. For example an instance of the currently executing project (from where your task is called from) could have properties overriden from command line or changed along the way your project is built, while your newly created project would have a default values of the properties after first MSBuild engine pass through the file.

您拥有的代码不是创建项目的副本,而是创建一个新实例,所以您不应该期望属性是相同的。例如当前执行的项目的一个实例(从你的任务在哪里叫)可以从命令行属性重载一路上或更改您的项目建设,而您新创建的项目会有一个默认值的属性首先MSBuild引擎通过文件之后。

#1


5  

You can not. MSBuild was specifically designed so that individual tasks only have access to the parameters that you explicitly passed to the task, and nothing else. That makes MSBuild files easier to read, now that you know that each task is only affected by what your specidied when you called this task.

你不能。MSBuild是专门设计的,以便每个任务只能访问显式传递给任务的参数,而不能访问其他参数。这使得MSBuild文件更易于阅读,因为您知道每个任务只受调用此任务时指定的内容的影响。

The code you have is not creating a copy of the project, it is creating a new instance, so you should not expect properties to be the same. For example an instance of the currently executing project (from where your task is called from) could have properties overriden from command line or changed along the way your project is built, while your newly created project would have a default values of the properties after first MSBuild engine pass through the file.

您拥有的代码不是创建项目的副本,而是创建一个新实例,所以您不应该期望属性是相同的。例如当前执行的项目的一个实例(从你的任务在哪里叫)可以从命令行属性重载一路上或更改您的项目建设,而您新创建的项目会有一个默认值的属性首先MSBuild引擎通过文件之后。