Open sandboxFocusImprove this doc
  • Article

Exposing configuration (before v2023.4)

Note

Starting with Metalama 2023.4, this approach is considered obsolete.

To establish a configuration API prior to Metalama 2023.4:

  1. Construct a class that inherits from ProjectExtension and includes a default constructor.
  2. If necessary, override the Initialize method, which accepts the IProject.
  3. In your aspect code, invoke the IProject.Extension<T>() method, where T represents your configuration class, to acquire the configuration object.
  4. If desired, devise an extension method for the IProject type to make your configuration API more discoverable. The class must be annotated with [CompileTime].
  5. For users to configure your aspect, they should implement a project fabric and access your configuration API using this extension method.

Example

1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3using System.Diagnostics;
4
5namespace Doc.AspectConfiguration;
6
7// The aspect itself, consuming the configuration.
8public class LogAttribute : OverrideMethodAspect
9{
10    public override dynamic? OverrideMethod()
11    {
12        var options = meta.Target.Method.Enhancements().GetOptions<LoggingOptions>();
13
14        var message = $"{options.Category}: Executing {meta.Target.Method}.";
15
16        switch ( options.Level!.Value )
17        {
18            case TraceLevel.Error:
19                Trace.TraceError( message );
20
21                break;
22
23            case TraceLevel.Info:
24                Trace.TraceInformation( message );
25
26                break;
27
28            case TraceLevel.Warning:
29                Trace.TraceWarning( message );
30
31                break;
32
33            case TraceLevel.Verbose:
34                Trace.WriteLine( message );
35
36                break;
37        }
38
39        return meta.Proceed();
40    }
41}
  • Navigation