UsingTask 元素 (MSBuild)UsingTask element (MSBuild)

Task 元素中參考的工作對應至包含工作實作的組件。Maps the task that is referenced in a Task element to the assembly that contains the task implementation.

<Project> <UsingTask><Project> <UsingTask>


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


與屬性和專案不同的是,將會使用適用于 TaskName第一個UsingTask 元素。若要覆寫工作,您必須在現有的 UsingTask之前定義新的。Unlike properties and items, the first UsingTask element that applies to a TaskName will be used; to override tasks you must define a new UsingTask before the existing one.

屬性和元素Attributes and elements

下列章節說明屬性、子元素和父元素。The following sections describe attributes, child elements, and parent elements.


屬性Attribute 描述Description
AssemblyName 需要 AssemblyName 屬性或 AssemblyFile 屬性。Either the AssemblyName attribute or the AssemblyFile attribute is required.

要載入之組件的名稱。The name of the assembly to load. AssemblyName 屬性接受強式名稱組件,雖然不需要強式名稱。The AssemblyName attribute accepts strong-named assemblies, although strong-naming is not required. 使用這個屬性就相當於使用 .NET 中的 Load 方法載入組件。Using this attribute is equivalent to loading an assembly by using the Load method in .NET.

如果使用 AssemblyFile 屬性,則您無法使用這個屬性。You cannot use this attribute if the AssemblyFile attribute is used.
AssemblyFile 需要 AssemblyNameAssemblyFile 屬性。Either the AssemblyName or the AssemblyFile attribute is required.

組件的檔案路徑。The file path of the assembly. 此屬性接受完整路徑或相對路徑。This attribute accepts full paths or relative paths. 相對路徑相對於專案檔案或目標檔案的目錄位置,其中會宣告 UsingTask 項目。Relative paths are relative to the directory of the project file or targets file where the UsingTask element is declared. 使用這個屬性就相當於使用 .NET 中的 LoadFrom 方法載入組件。Using this attribute is equivalent to loading an assembly by using the LoadFrom method in .NET.

如果使用 AssemblyName 屬性,則您無法使用這個屬性。You cannot use this attribute if the AssemblyName attribute is used.
TaskFactory 選擇性屬性。Optional attribute.

指定組件中的類別,該組件負責產生指定 Task 名稱的執行個體。Specifies the class in the assembly that is responsible for generating instances of the specified Task name. 使用者也可以指定 TaskBody 做為子項目,工作 Factory 會接收並用來產生工作。The user may also specify a TaskBody as a child element that the task factory receives and uses to generate the task. TaskBody 的內容專屬於工作 Factory。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. 如果有模稜兩可的情況,MSBuild 會選擇任意的相符項目,這樣可能會產生非預期的結果。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
ParameterGroupParameterGroup 在指定的 TaskFactory 產生之工作上顯示的參數集。The set of parameters that appear on the task that is generated by the specified TaskFactory.
TaskTask 傳遞至 TaskFactory 以產生工作之執行個體的資料。The data that is passed to the TaskFactory to generate an instance of the task.

父元素Parent elements

元素Element 描述Description
專案Project MSBuildMSBuild 專案檔案的必要根項目。Required root element of an MSBuildMSBuild project file.


您可以在專案檔所包含 (直接包含或透過匯入的專案檔來包含) 的 UsingTask 元素中,參考環境變數、命令列屬性、專案層級屬性及專案層級項目。Environment variables, command-line properties, project-level properties, and project-level items can be referenced in the UsingTask elements included in the project file either directly or through an imported project file. 如需詳細資訊,請參閱工作For more information, see Tasks.


如果 UsingTask 元素來自已經向 MSBuild 引擎全域註冊的其中一個 .tasks 檔案,則專案層級屬性和項目沒有任何意義。Project-level properties and items have no meaning if the UsingTask element is coming from one of the .tasks files that are globally registered with the MSBuild engine. 專案層級值對 MSBuild 而言不是全域的。Project-level values are not global to MSBuild.

在 MSBuild 4.0 中,使用工作可以從 .overridetask 檔案載入。In MSBuild 4.0, using tasks can be loaded from .overridetask files.


下列範例示範如何使用具有 AssemblyName 屬性的 UsingTask 項目。The following example shows how to use the UsingTask element with an AssemblyName attribute.

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


下列範例示範如何使用具有 AssemblyFile 屬性的 UsingTask 項目。The following example shows how to use the UsingTask element with an AssemblyFile attribute.

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

另請參閱See also