[英]How do I access current Project context within a custom Task?
How do I access current Project context within a custom Task in MSBuild? 如何在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.
最初,似乎好像ProjectCollection上的GlobalProjectCollection引用将允许访问,但是至少在从命令行运行MSBuild时,它似乎为空。
I can currently get new Project instance based off of the current project file in the following manner: 我目前可以通过以下方式从当前项目文件中获取新的Project实例:
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?
我如何才能访问从中调用任务的项目?
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.
MSBuild是专门设计的,因此单个任务只能访问您显式传递给任务的参数,而不能访问其他参数。 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文件更易于阅读。
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引擎通过该文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.