Proposal: "Template" members #3612
Replies: 1 comment
-
IMO this approach is worse than IMO I'd almost take an opposite approach, instead of having the method in question being completely rewritten, have it set in stone and the only thing the generator can do is to emit code "around" the method. That better fits AOP, in my opinion, and would leave what the developer wrote effectively intact. I have no idea what that would look like, though. I do think it's great to reopen the conversation around source generators and AOP as I feel that the rejection of |
Beta Was this translation helpful? Give feedback.
-
Currently source generators are unable to alter existing code, and can only add new files, which severely limits its applicability for AOP focused scenarios.
This proposal suggests a method to allow this using a new feature: template members.
A template member is not included in the final IL, cannot be referenced, and does not participate in Name Lookup or overload resolution.
This allows source generators to use the method as a template for anything it wants to generate. For example it could be used to automatically add
ConfigureAwait(false)
.Developer writes:
Source generator can then generate:
Issues
Doesn't compose:
If multiple source generators run on the same template, there's no way for them to combine together. The same is currently true for generators which rely on partial methods, but AOP scenarios make this more likely.
Open Questions
Should a template member be allowed to contain any errors:
option 1: No
option 2: Yes, so long as any generated code does not contain any errors
option 3: Yes, but the source generator has to explicitly supress any errors.
Beta Was this translation helpful? Give feedback.
All reactions