Open sandboxFocusImprove this doc

Namespace Metalama.Framework.Code.Invokers

This namespace defines invokers. Invokers are objects that generate syntax to invoke methods or access properties, fields, or events.

Example

The following aspect displays the value of all fields and automatic properties.

1using Metalama.Framework.Aspects;
2using System;
3
4namespace Doc.PrintFieldValues;
5
6internal class PrintFieldValuesAttribute : OverrideMethodAspect
7{
8    public override dynamic? OverrideMethod()
9    {
10        foreach ( var fieldOrProperty in meta.Target.Type.FieldsAndProperties )
11        {
12            if ( fieldOrProperty is { IsImplicitlyDeclared: false, IsAutoPropertyOrField: true } )
13            {
14                var value = fieldOrProperty.Value;
15                Console.WriteLine( $"{fieldOrProperty.Name}={value}" );
16            }
17        }
18
19        return meta.Proceed();
20    }
21}
Source Code
1namespace Doc.PrintFieldValues;
2


3internal class Foo
4{
5    private readonly int _a;
6
7    public string? B { get; set; }
8
9    private static readonly int _c;
10
11    [PrintFieldValues]
12    public void Bar() { }
13}
Transformed Code
1using System;
2
3namespace Doc.PrintFieldValues;
4
5internal class Foo
6{
7    private readonly int _a;
8
9    public string? B { get; set; }
10
11    private static readonly int _c;
12
13    [PrintFieldValues]
14    public void Bar()
15    {
16        var value = _a;
17        Console.WriteLine($"_a={value}");
18        var value_1 = _c;
19        Console.WriteLine($"_c={value_1}");
20        var value_2 = B;
21        Console.WriteLine($"B={value_2}");
22    }
23}

Namespace members

Interfaces

IConstructorInvoker

Allows invocation of the constructor.

IEventInvoker

Allows adding/removing delegates to/from events.

IFieldOrPropertyInvoker

Allows accessing the the value of fields or properties through the Value property of the IExpression interface. By default, the target instance of the field or property is this unless the property is static, and the base implementation of the property is invoked, i.e. the implementation before the current aspect layer. To change the default values, or to use the ?. null-conditional operator, use the With(InvokerOptions) method.

IIndexerInvoker

Allows accessing the value of indexers.

IMethodInvoker

Allows invocation of the method.

Enums

InvokerOptions

Options that influence the behavior of invokers, i.e. IMethodInvoker, IFieldOrPropertyInvoker, IEventInvoker or IIndexerInvoker.