Open sandboxFocusImprove this doc

LAMA0018: A local function can't be invoked from generated code

Severity Error
Reported by Metalama.Framework
Message '<Member>' is a local function, so it cannot be invoked on an instance or a type.

Cause

The code model can represent a local function as an IMethod whose MethodKind is LocalFunction. Metalama reports LAMA0018 when your template or compile-time code uses the invoker API of such a method, for instance Invoke, to generate a call to it. In the message, <Member> is the local function.

Invokers generate a call on a receiver: an instance, this, or a type name for static members. A local function has no receiver, so Metalama rejects the invocation. Besides, a local function is only visible inside the body that declares it, so the code generated by an aspect, which belongs to another member or another block, couldn't call it anyway.

Local functions rarely appear in the code model, because aspects can't be applied to them. This diagnostic is reported only when compile-time code reaches a local function through an indirect path, so it isn't shown in an example.

Resolution

Don't generate calls to local functions of the source code. Instead, do one of the following:

  • Turn the local function into a regular method of the type, then invoke this method.
  • Generate the logic you need in your template, or introduce a helper method with an aspect and invoke the introduced method.
  • If you enumerate methods from the code model, skip the methods whose MethodKind is LocalFunction.

See also