Skip to content

Add HandlerTemplate to ScanForTypesAttribute#58

Merged
Dreamescaper merged 7 commits into
mainfrom
copilot/add-template-handler-per-type
Apr 11, 2026
Merged

Add HandlerTemplate to ScanForTypesAttribute#58
Dreamescaper merged 7 commits into
mainfrom
copilot/add-template-handler-per-type

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 9, 2026

ScanForTypesAttribute lacked a way to generate per-type expressions beyond method invocations, forcing users to fall back to Activator when constructors require arguments.

Changes

  • New HandlerTemplate property on ScanForTypesAttribute: a string template where T (word-boundary matched) is replaced with the fully-qualified name of each found type
    • For collection-returning methods: template becomes each array element expression
    • For void / first-param-returning methods: template becomes a statement (; appended if absent)
    • Incompatible with Handler — new DI0016 diagnostic enforces this
  • MissingCustomHandlerOnGenerateServiceHandler (DI0013) no longer fires when HandlerTemplate is set
  • Template placeholder uses \bT\b regex (static compiled) for safe word-boundary replacement

Usage

// Collection return — generate instances with constructor args
[ScanForTypes(AssignableTo = typeof(BaseType), HandlerTemplate = "new T(argument)")]
public partial BaseType[] GetTypeInstances(string argument);

// Void method — generate statements
[ScanForTypes(AssignableTo = typeof(BaseType), HandlerTemplate = "registry.Add(new T(argument))")]
public partial void RegisterInstances(BaseTypeRegistry registry, string argument);

Copilot AI changed the title [WIP] Add ability to set template of handler per each type Add HandlerTemplate to ScanForTypesAttribute Apr 9, 2026
Copilot AI requested a review from Dreamescaper April 9, 2026 17:21
@Dreamescaper Dreamescaper marked this pull request as ready for review April 11, 2026 15:47
@Dreamescaper
Copy link
Copy Markdown
Owner

@copilot
update README.md

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 11, 2026

@copilot
update README.md

Done in ea6369c. Added a new "Create instances using constructors with parameters" example section showing both collection-returning and void-method usage, and added HandlerTemplate to the ScanForTypes parameters table.

@Dreamescaper
Copy link
Copy Markdown
Owner

@copilot
Make README info less about constructors specifically, and more about any non-standard cases, when Handler is not flexible enough. Constructors would be just one example. Static methods with multiple parameters, typeof manipulations etc.

Copy link
Copy Markdown
Contributor Author

Copilot AI commented Apr 11, 2026

@copilot
Make README info less about constructors specifically, and more about any non-standard cases, when Handler is not flexible enough. Constructors would be just one example. Static methods wit...

Updated in 6af0890. The section is now titled "Use a template expression for each matched type" and leads with the general concept before showing three distinct examples: constructor args, a static factory method with multiple parameters, and typeof manipulations — with the void-method variant at the end.

Comment thread README.md Outdated
Comment thread README.md Outdated
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add ability to set template of handler per each type

2 participants