views:

22

answers:

1

When implementing a microsoft.build.utilities.task how to i get access to the various environmental variables of the build?

For example "TargetPath"

I know i can pass it in as part of the task XML

<MyTask TargetPath="$(TargetPath)" />

But i don't want to force the consumer of the task to have to do that if I can access the variable in code.

http://msdn.microsoft.com/en-us/library/microsoft.build.utilities.task.aspx

A: 

You can not do this easily and you shouldn't do it. A task shouldn't know its context of execution, and should work with its input parameters.

Disclaimer : Don't do it!

If you really want to do it, you would need to reparse the project file with something like that.

public override bool Execute()
{
  string projectFile = BuildEngine.ProjectFileOfTaskNode;

  Engine buildEngine = new Engine(System.Runtime.InteropServices.RuntimeEnvironment.GetRuntimeDirectory());

  Project project = new Project(buildEngine);
  project.Load(projectFile);
  foreach(var o in project.EvaluatedProperties)
  {
    // Use properties
  }

  // Do what you want

  return true;
}
madgnome
madgnome: in retrospect i think you are correct and i will avoid doing this.
Simon