Open sandboxFocusImprove this doc

LAMA0025: The template name doesn't match any member

Severity Error
Reported by Metalama.Framework
Message The type '<AspectType>' does not contain any template member named '<MethodName>'. Check the template name, for instance by using the 'nameof' operator.

Cause

When you add advice programmatically, for instance in the BuildAspect method, you identify the template by its name, as a string. Metalama then looks for a member of that name in the aspect class, or in the template provider class.

Metalama reports LAMA0025 when the class, <AspectType> in the message, has no member named <MethodName>. This typically happens when the string contains a typo, or when the template was renamed but the string wasn't updated. Metalama also reports this error when a template invoked by name from another template doesn't exist. The error is reported on the target declaration of the aspect.

In the following example, the aspect passes "LogTemplate" to the Override method, but the template is named OverrideTemplate.

1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3using System;
4
5namespace Doc.LAMA0025.Error;
6
7public class LogAttribute : MethodAspect
8{
9    public override void BuildAspect( IAspectBuilder<IMethod> builder )
10    {
11        // Error: the aspect has no template named 'LogTemplate'.
12        builder.Override( "LogTemplate" );
13    }
14
15    [Template]
16    private dynamic? OverrideTemplate()
17    {
18        Console.WriteLine( $"Executing {meta.Target.Method}." );
19
20        return meta.Proceed();
21    }
22}
23
1namespace Doc.LAMA0025.Error;
2
3internal class Calculator
4{
5    [Log]
    Error LAMA0025: The type 'Log' does not contain any template member named 'LogTemplate'. Check the template name, for instance by using the 'nameof' operator.

6    public int Add( int a, int b ) => a + b;
7}
8

Resolution

Pass the name of an existing template member. To avoid this error in the future, use the nameof operator instead of a string literal, so that the C# compiler checks the name and updates it when you rename the template.

1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3using System;
4
5namespace Doc.LAMA0025.Fixed;
6
7public class LogAttribute : MethodAspect
8{
9    public override void BuildAspect( IAspectBuilder<IMethod> builder )
10    {
11        // Fixed: nameof guarantees that the template name matches an existing member.
12        builder.Override( nameof(OverrideTemplate) );
13    }
14
15    [Template]
16    private dynamic? OverrideTemplate()
17    {
18        Console.WriteLine( $"Executing {meta.Target.Method}." );
19
20        return meta.Proceed();
21    }
22}
23
Source Code
1namespace Doc.LAMA0025.Fixed;
2


3internal class Calculator
4{
5    [Log]
6    public int Add( int a, int b ) => a + b;
7}
8
Transformed Code
1using System;
2
3namespace Doc.LAMA0025.Fixed;
4
5internal class Calculator
6{
7    [Log]
8    public int Add(int a, int b)
9    {
10        Console.WriteLine("Executing Calculator.Add(int, int).");
11        return a + b;
12    }
13}
14

See also