Task.RegisterTask Method
Definition
Important
Some information relates to prerelease product that may be substantially modified before it’s released. Microsoft makes no warranties, express or implied, with respect to the information provided here.
Overloads
| Name | Description |
|---|---|
| RegisterTask(String, Func<ITask>) |
Registers a task under the name a target uses to invoke it (the |
| RegisterTask<T>() |
Registers a task type under its type name - |
| RegisterTask<TTask>(Func<TaskEnvironment,TTask>) |
Registers a task type under its type name (the name a target uses to invoke it) with a factory that receives the TaskEnvironment the engine is running the task with, so the task can consume it during construction. Use this for tasks whose property defaults (or other constructor logic) depend on the TaskEnvironment. |
RegisterTask(String, Func<ITask>)
- Source:
- Task.cs
Registers a task under the name a target uses to invoke it (the TaskName of a
<UsingTask>) with an explicit factory, so construction is fully reflection-free (the
host supplies the constructor). Use this for tasks without a public parameterless constructor or
that need custom construction.
public:
static void RegisterTask(System::String ^ taskName, Func<Microsoft::Build::Framework::ITask ^> ^ factory);
public static void RegisterTask(string taskName, Func<Microsoft.Build.Framework.ITask> factory);
static member RegisterTask : string * Func<Microsoft.Build.Framework.ITask> -> unit
Public Shared Sub RegisterTask (taskName As String, factory As Func(Of ITask))
Parameters
- taskName
- String
The name a target uses to invoke the task. This is the TaskName of the corresponding
<UsingTask> (typically the task's class name, optionally namespace-qualified).
Remarks
Construction is reflection-free, but binding the task's parameters still reflects over its properties. Because the task type is not statically known through this overload, the host is responsible for preserving that type's public properties under trimming (for example by also registering it through RegisterTask<T>(), which roots them). The generic overload is the fully trim-safe path.
Intended to be called once per task during host initialization, before the first build. This method is thread-safe; registering the same name again replaces the previous registration. A registered name takes precedence over a project-level <UsingTask> of the same name.
Applies to
RegisterTask<T>()
- Source:
- Task.cs
Registers a task type under its type name - typeof(T).Name, the name a target uses to invoke
it (the TaskName of a <UsingTask>) - so MSBuild can instantiate and run it
without loading its assembly or resolving its type by reflection - the path required in a trimmed or
Native AOT host.
public:
generic <typename T>
where T : Microsoft::Build::Framework::ITask static void RegisterTask();
public static void RegisterTask<T>() where T : Microsoft.Build.Framework.ITask;
static member RegisterTask : unit -> unit (requires 'T :> Microsoft.Build.Framework.ITask)
Public Shared Sub RegisterTask(Of T As ITask) ()
Type Parameters
- T
The task type to register. The registration key is typeof(T).Name. It must have either a
public parameterless constructor or a public constructor taking a single TaskEnvironment
parameter (preferred when present). The [DynamicallyAccessedMembers] roots the type's public
constructors and properties so a trimmer preserves them, keeping both construction and parameter
binding working.
Remarks
Intended to be called once per task during host initialization, before the first build. This method is thread-safe; registering the same name again replaces the previous registration. A registered name takes precedence over a project-level <UsingTask> of the same name, and registration does not participate in Runtime/Architecture task-identity selection - the registered task is always the one the engine constructs.
Applies to
RegisterTask<TTask>(Func<TaskEnvironment,TTask>)
- Source:
- Task.cs
Registers a task type under its type name (the name a target uses to invoke it) with a factory that receives the TaskEnvironment the engine is running the task with, so the task can consume it during construction. Use this for tasks whose property defaults (or other constructor logic) depend on the TaskEnvironment.
public:
generic <typename TTask>
where TTask : Microsoft::Build::Framework::ITask static void RegisterTask(Func<Microsoft::Build::Framework::TaskEnvironment ^, TTask> ^ factory);
public static void RegisterTask<TTask>(Func<Microsoft.Build.Framework.TaskEnvironment,TTask> factory) where TTask : Microsoft.Build.Framework.ITask;
static member RegisterTask : Func<Microsoft.Build.Framework.TaskEnvironment, 'ask (requires 'ask :> Microsoft.Build.Framework.ITask)> -> unit (requires 'ask :> Microsoft.Build.Framework.ITask)
Public Shared Sub RegisterTask(Of TTask As ITask) (factory As Func(Of TaskEnvironment, TTask))
Type Parameters
- TTask
The task type to register. The registration key is typeof(TTask).Name - the name a target
uses to invoke the task. The [DynamicallyAccessedMembers] roots the type's public properties
so a trimmer preserves them, keeping parameter binding working; the supplied factory handles
construction, so this overload is fully trim-safe.
Parameters
- factory
- Func<TaskEnvironment,TTask>
A delegate that creates a new instance of the task given the current TaskEnvironment.
Remarks
Construction is fully reflection-free (the factory supplies it) and, because the task type is statically known, parameter binding is trim-safe as well. The engine still assigns the task's TaskEnvironment property after construction, so a task that reads the injected environment in its constructor should also store it into that property.
Because the factory returns TTask, the compiler infers TTask from the factory, so callers normally do not write the type argument explicitly (for example RegisterTask(env => new MyTask(env))).
Intended to be called once per task during host initialization, before the first build. This method is thread-safe; registering the same name again replaces the previous registration. A registered name takes precedence over a project-level <UsingTask> of the same name.