Use HandlerTemplate for complex type mapping
mainWhen a standard Handler is insufficient (e.g., you need to pass extra arguments, call static methods with multiple parameters, or perform typeof manipulations), use HandlerTemplate. The placeholder T is replaced with the fully-qualified name of each matched type at code-generation time.
Examples of HandlerTemplate usage:
Instantiate types with constructor arguments:
[ScanForTypes(AssignableTo = typeof(IPlugin), HandlerTemplate = "new T(options)")]
public static partial IPlugin[] CreatePlugins(PluginOptions options);Call a static factory method with multiple parameters:
[ScanForTypes(AssignableTo = typeof(IPipelineStep), HandlerTemplate = "T.Create(context, logger)")]
public static partial IPipelineStep[] BuildSteps(PipelineContext context, ILogger logger);Build descriptor objects using typeof(T):
[ScanForTypes(AssignableTo = typeof(ICommandHandler), HandlerTemplate = "new HandlerDescriptor(typeof(T), category)")]
public static partial HandlerDescriptor[] GetDescriptors(string category);Use with void methods (each expression becomes a statement):
[ScanForTypes(AssignableTo = typeof(IPlugin), HandlerTemplate = "registry.Add(new T(options))")]
public static partial void RegisterPlugins(PluginRegistry registry, PluginOptions options);// Instantiate types with constructor arguments
[ScanForTypes(AssignableTo = typeof(IPlugin), HandlerTemplate = "new T(options)")]
public static partial IPlugin[] CreatePlugins(PluginOptions options);
// Call a static factory method that takes multiple parameters
[ScanForTypes(AssignableTo = typeof(IPipelineStep), HandlerTemplate = "T.Create(context, logger)")]
public static partial IPipelineStep[] BuildSteps(PipelineContext context, ILogger logger);
// Build descriptor objects using typeof(T) and additional context
[ScanForTypes(AssignableTo = typeof(ICommandHandler), HandlerTemplate = "new HandlerDescriptor(typeof(T), category)")]
public static partial HandlerDescriptor[] GetDescriptors(string category);
// Use with void methods (each expanded expression becomes a statement)
[ScanForTypes(AssignableTo = typeof(IPlugin), HandlerTemplate = "registry.Add(new T(options))")]
public static partial void RegisterPlugins(PluginRegistry registry, PluginOptions options);