Delen via


UsingTask Element (MSBuild)

Note

This article applies to Visual Studio 2015. If you're looking for the latest Visual Studio documentation, see Visual Studio documentation. We recommend upgrading to the latest version of Visual Studio. Download it here

Maps the task that is referenced in a Task element to the assembly that contains the task implementation.

<Project>
<UsingTask>

Syntax

<UsingTask TaskName="TaskName"  
    AssemblyName = "AssemblyName"   
    TaskFactory = "ClassName"  
    Condition="'String A'=='String B'" />  

Attributes and Elements

The following sections describe attributes, child elements, and parent elements.

Attributes

Attribute Description
AssemblyName Either the AssemblyName attribute or the AssemblyFile attribute is required.

The name of the assembly to load. The AssemblyName attribute accepts strong-named assemblies, although strong-naming is not required. Using this attribute is equivalent to loading an assembly by using the Load method in .NET.

You cannot use this attribute if the AssemblyFile attribute is used.
AssemblyFile Either the AssemblyName or the AssemblyFile attribute is required.

The file path of the assembly. This attribute accepts full paths or relative paths. Relative paths are relative to the directory of the project file or targets file where the UsingTask element is declared. Using this attribute is equivalent to loading an assembly by using the LoadFrom method in .NET.

You cannot use this attribute if the AssemblyName attribute is used.
TaskFactory Optional attribute.

Specifies the class in the assembly that is responsible for generating instances of the specified Task name. The user may also specify a TaskBody as a child element that the task factory receives and uses to generate the task. The contents of the TaskBody are specific to the task factory.
TaskName Required attribute.

The name of the task to reference from an assembly. If ambiguities are possible, this attribute should always specify full namespaces. If there are ambiguities, MSBuild chooses an arbitrary match, which could produce unexpected results.
Condition Optional attribute.

The condition to evaluate. For more information, see Conditions.

Child Elements

Element Description
ParameterGroup The set of parameters that appear on the task that is generated by the specified TaskFactory.
Task The data that is passed to the TaskFactory to generate an instance of the task.

Parent Elements

Element Description
Project Required root element of an MSBuild project file.

Remarks

Environment variables, command-line properties, and project-level properties can be referenced anywhere in the UsingTask element if it appears in the project file either explicitly or through an imported project file. For more information, see Tasks.

Note

Project-level properties have no meaning if the UsingTask element is coming from one of the .tasks files that are globally registered with the MSBuild engine. Project-level properties are not global to MSBuild.

In MSBuild 4.0, using tasks can be loaded from .overridetask files.

Example

The following example shows how to use the UsingTask element with an AssemblyName attribute.

<UsingTask TaskName="MyTask" AssemblyName="My.Assembly" TaskFactory="MyTaskFactory">  
       <ParameterGroup>  
              <Parameter1 ParameterType="System.String" Required="False" Output="False"/>  
              <Parameter2 ParameterType="System.Int" Required="True" Output="False"/>  
              ...  
</ParameterGroup>  
       <TaskBody>  
      ... Task factory-specific data ...  
       </TaskBody>  
</UsingTask>  

Example

The following example shows how to use the UsingTask element with an AssemblyFile attribute.

<UsingTask TaskName="Email"  
              AssemblyFile="c:\myTasks\myTask.dll" />  

See Also

Tasks
Task Reference
Project File Schema Reference