Metalama 1.0 / / Metalama Documentation / Aspect API Documentation / Metalama.​Framework.​Code.​Invokers

Namespace Metalama.Framework.Code.Invokers

This namespace defines invokers, which are objects that generate syntax that invokes methods or accesses properties, fields or events.

Where it makes sense, declarations expose an invoker factory (IInvokerFactory<T>) on their Invokers property. The invoker factory interface has two properties:

  • Final is equivalent to the this keyword in C#. It allows you to access the last override of the semantic.

  • Base is equivalent to the base keyword in C#. It allows you to access the implementation prior to the current aspect layer.

  • ConditionalFinal and ConditionalBase generate a .? null-conditional access instead of ..

Class Diagram

classDiagram class IInvokerFactory~T~ { T? Base T? ConditionalBase T Final T ConditionalBase } IInvokerFactory~T~ --> IInvoker: exposes IMethod --> IInvokerFactory~IMethodInvoker~: exposes IEvent --> IInvokerFactory~IEventInvoker~: exposes IFieldOrProperty --> IInvokerFactory~IFieldOrPropertyInvoker~: exposes IMethodInvoker <|-- IInvoker IFieldOrPropertyInvoker <|-- IInvoker IPropertyInvoker <|-- IFieldOrPropertyInvoker IEventInvoker <|-- IInvoker class IMethod { Invokers } class IEvent { Invokers } class IFieldOrProperty { Invokers } class IMethodInvoker { +Invoke() } class IFieldOrPropertyInvoker { +GetValue() +SetValue() } class IPropertyInvoker { +GetIndexerValue() +SetIndexerValue() } class IEventInvoker { +AddDelegate() +RemoveDelegate() }

Example

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

using Metalama.Framework.Aspects;
using System;

namespace Doc.PrintFieldValues
{
    internal class PrintFieldValuesAttribute : OverrideMethodAspect
    {
        public override dynamic? OverrideMethod()
        {
            foreach ( var fieldOrProperty in meta.Target.Type.FieldsAndProperties )
            {
                if ( fieldOrProperty.IsAutoPropertyOrField )
                {
                    var value = fieldOrProperty.Invokers.Final.GetValue( fieldOrProperty.IsStatic ? null : meta.This );
                    Console.WriteLine( $"{fieldOrProperty.Name}={value}" );
                }
            }

            return meta.Proceed();
        }
    }
}
namespace Doc.PrintFieldValues
{
    internal class Foo
    {
        private readonly int _a;

        public string? B { get; set; }

        private static readonly int _c;

        [PrintFieldValues]
        public void Bar() { }
    }
}
using System;

namespace Doc.PrintFieldValues
{
    internal class Foo
    {
        private readonly int _a;

        public string? B { get; set; }

        private static readonly int _c;

        [PrintFieldValues]
        public void Bar()
        {
            var value = _a;
            Console.WriteLine($"_a={value}");
            var value_1 = _c;
            Console.WriteLine($"_c={value_1}");
            var value_2 = B;
            Console.WriteLine($"B={value_2}");
            return;
        }
    }
}

Namespace members

Classes

InvokerOperator

Kinds of member access operator: . or ?..

InvokerOrder

Enumeration of orders for invokers.

Interfaces

IEventInvoker

Allows adding/removing delegates to/from events.

IFieldOrPropertyInvoker

Allows accessing the the value of fields or properties.

IIndexerInvoker

Allows accessing the value of indexers.

IInvoker

A base interface for all invokers, which are a mechanism to invoke the semantic of declarations and to specify which layer of overrides should be invoked.

IInvokerFactory<T>

Gives access to invokers.

IMethodInvoker

Allows invocation of the method.