Aspects generally do not get access to the underlying Roslyn code model of the current project. The most serious consequence of this limitation is that aspects have no access to the syntax tree of method implementations -- indeed, the Metalama.Framework.Code only exposes declarations, but not implementation syntax.
If your aspect needs to read the syntax tree, it can do it through an SDK service. An SDK service is an interface that is defined in a normal project (and is therefore usable by the aspect) but is implemented in a weaver project (and can therefore access the Roslyn API).
Creating an SDK service
Step 1. Create the solution scaffolding
This step is described in Creating a Metalama SDK solution structure.
Step 2. Define the interface
In the public project created in the previous step, define the service interface. The interface can be internal if the public project has exposed its internals to the weaver project.
Step 3. Implement the interface
In the weaver project created in Step 1:
- Add a class that implements the service interface defined in the previous step.
- Make sure the class is
- Add the MetalamaPlugInAttribute custom attributes to this class.
Consuming an SDK service
In your aspect class, you can get your service implementation from the IServiceProvider exposed on the IProject interface. This interface is available from any declaration of the code model through
declaration.Compilation.Project, or from the MetalamaExecutionContext static class.
SDK services are not available at design time.