Exposing an aspect as a live template
A live template is a custom Code Action available in the code editor menu like other code suggestions or refactoring actions offered by the IDE. For details about using live templates, see Using live templates.
Live templates are built with the Metalama Aspect Framework. Instead of being executed at compile time by the compiler, they are interactively applied by the user in the editor, and they modify the source code.
Note
A fundamental characteristic of an aspect is that it is applied at compile time and does not affect the source code. Therefore, a live template cannot be called an aspect, even if it is built with the aspect framework. To avoid confusion, we suggest not referring to live templates as aspects in your communication.
To write a live template
- Write an aspect class as usual, with the following differences:
- The aspect class does not need to be derived from
System.Attribute
. - Pay attention to generating idiomatic C# code.
- Diagnostics reported by the aspect will be ignored.
- Aspect ordering and requirements will be ignored.
- The aspect class does not need to be derived from
- Make sure that the aspect class has a default constructor.
- Annotate the class with
[EditorExperience(SuggestAsLiveTemplate = true)]
. - Make sure that you properly define the aspect eligibility, so the code refactoring will only be suggested for relevant declarations. See Defining the eligibility of aspects for details.