Skip to content

Add ability to set template of handler per each type #57

@Dreamescaper

Description

@Dreamescaper

Let's say we want to do something, which is not possible via generic method. The easiest example - return created instances for each found type, and those types have constructors with parameters.
Currently you have to resort to using Activator (which is not compile safe).

public BaseType[] GetTypeInstances(string argument)
{
	return [
		new TypeA(argument),
	    new TypeB(argument)
	];
}

I suggest to parameter HandlerTemplate to ScanForTypesAttribute. Source generator should replace T with full name of found type:

[ScanForTypes(AssignableTo=typeof(BaseType), HanderTemplate = "new T(argument)")]
public partial BaseType[] GetTypeInstances(string argument);

It should support void methods (or methods returning first argument) as well:

[ScanForTypes(AssignableTo=typeof(BaseType), HanderTemplate = "registry.Add(new T(argument))")]
public partial void GetTypeInstances(BaseTypeRegistry registry, string argument);

For this case I suggest to add ';' to the end of the string if it is not there.

It is not supported to have both Handler and HandlerTemplate.

Metadata

Metadata

Labels

No labels
No labels

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions