Create a gist now

Instantly share code, notes, and snippets.

An extension method for ProjectTargetElement that ensures the project has the correct UsingTask
using System.Linq;
using Microsoft.Build.Construction;
static class ProjectTargetElementExtensions
{
public static ProjectTaskElement AddTask<TTask>(this ProjectTargetElement projectTargetElement)
{
string taskName = typeof (TTask).Name;
ProjectRootElement projectRootElement = projectTargetElement.ContainingProject;
if (!projectRootElement.UsingTasks.Any(ut => ut.TaskName == taskName))
{
projectRootElement.AddUsingTask<TTask>();
}
return projectTargetElement.AddTask(taskName);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment