Code templates are the foundation of how aspects generate and transform code in Metalama. Templates are written in T#, a dialect of C# that combines compile-time logic with run-time code generation. While T# syntax is fully compatible with C#, the compilation process is different: T# code executes at compile-time to generate the C# code that will run in your application.
Templates let you write code that analyzes your codebase at compile-time and generates new code or modifies existing code based on that analysis. This powerful mechanism enables you to automate repetitive coding patterns, enforce architectural rules, and implement cross-cutting concerns.
This chapter includes the following articles:
| Article | Description |
|---|---|
| T# templates: overview | This article provides an introduction to T#, the template language for Metalama. |
| Writing compile-time code | This article outlines the subset of the C# language that can be used as compile-time code and illustrates how to create templates with rich compile-time logic. |
| Dynamic typing in templates |
This article explains the use of dynamic typing in templates.
|
| Generating run-time expressions | This article details different techniques for generating expressions dynamically. |
| Generating code based on the code model |
This article explains how to generate expressions that access members once you have their compile-time IMethod, IProperty, and similar objects.
|
| Generating run-time statements | This article lists techniques for generating statements dynamically. |
| Generating System.Reflection objects |
This article clarifies how to generate run-time System.Reflection objects for compile-time Metalama.Framework.Code objects from a template.
|
| Template parameters and type parameters |
This article describes how to pass parameters, including generic parameters, from the BuildAspect method to the template.
|
| Calling auxiliary templates | This article explains how templates can invoke other templates, referred to as auxiliary templates. |
| Debugging aspects | This article provides guidance on debugging templates. |
Getting started
To begin working with templates, start with T# templates: overview to understand the fundamentals of T# and how compile-time and run-time code work together. Once you're comfortable with the basics, explore the other articles in this chapter to learn about specific techniques and advanced features.