简体   繁体   English

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

[英]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.

相关问题 如何在当前上下文中访问WCF服务实例? - How do I get access to the WCF service instance in the current context? 如何在当前项目中访问数据集中的数据? - How to access data in a DataSet within current project? 创建自定义MSBuild任务时如何从C#代码获取当前项目目录? - How do you get the current project directory from C# code when creating a custom MSBuild task? 如何在自定义ActionFilterAttribute中访问模型和元数据? - How do I access the model and Metadata within a custom ActionFilterAttribute? 如何在Task.Factory.StartNew中访问HttpContext.Current? - How do I access HttpContext.Current in Task.Factory.StartNew? 如何从其他类中访问我的“当前屏幕”变量? - How do I access my “Current Screen” variable from within other classes? 如何解决“当前上下文中不存在名称'页面'” - How do I fix “The name 'page' does not exist in the current context” 如何修复“当前上下文中不存在名称‘脚本’”? - how do I fix "The name 'script' does not exist in the current context"? 如何确定当前数据上下文中是否存在实体? - How do I determine if an entity exists in the current data context? 如何从“模板化” @helper中访问MVC中的静态自定义帮助器? - How do I access a static Custom Helper in MVC from within a “Templated” @helper?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM