Open sandboxFocusImprove this doc

LAMA0033: The member used as a template doesn't have the [Template] attribute

Severity Error
Reported by Metalama.Framework
Message The class '<ClassName>' defines a member named '<MemberName>', but the member is not annotated with the [Template] custom attribute.

Cause

When an aspect calls an advice method from BuildAspect, for instance IntroduceMethod or Override, it identifies the template by the name of a member of the aspect class, typically with nameof. Metalama treats a member as a template only if it has the TemplateAttribute custom attribute, or another template attribute.

Metalama reports LAMA0033 when the aspect class has a member of the given name, but this member doesn't have a template attribute. In the message, <ClassName> is the full name of the aspect class and <MemberName> is the name of the member.

You typically get this error when you forget the [Template] attribute on a method that doesn't use any meta API. If the method uses meta, Metalama reports a different error first, because meta can only be used in templates.

In the following example, the Describe aspect introduces the Describe method, but this method doesn't have the [Template] attribute.

1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3
4namespace Doc.LAMA0033.Error;
5
6public class DescribeAttribute : TypeAspect
7{
8    public override void BuildAspect( IAspectBuilder<INamedType> builder )
9    {
10        builder.IntroduceMethod( nameof(this.Describe) );
11    }
12
13    // Error: the method is used as a template but has no [Template] attribute.
14    public string Describe() => "This object is enhanced by the [Describe] aspect.";
15}
16
1namespace Doc.LAMA0033.Error;
2
3[Describe]
Error LAMA0033: The class 'Doc.LAMA0033.Error.DescribeAttribute' defines a member named 'Describe', but the member is not annotated with the [Template] custom attribute.

4internal partial class Calculator
5{
6    public int Add( int a, int b ) => a + b;
7}
8

Resolution

Add the [Template] attribute to the member, as in the following example. If you passed the wrong name to the advice method, pass the name of an existing template instead.

1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3
4namespace Doc.LAMA0033.Fixed;
5
6public class DescribeAttribute : TypeAspect
7{
8    public override void BuildAspect( IAspectBuilder<INamedType> builder )
9    {
10        builder.IntroduceMethod( nameof(this.Describe) );
11    }
12
13    // Fixed: the method has the [Template] attribute.
14    [Template]
15    public string Describe() => "This object is enhanced by the [Describe] aspect.";
16}
17
Source Code
1namespace Doc.LAMA0033.Fixed;
2

3[Describe]
4internal partial class Calculator
5{
6    public int Add( int a, int b ) => a + b;
7}




8
Transformed Code
1namespace Doc.LAMA0033.Fixed;
2
3[Describe]
4internal partial class Calculator
5{
6    public int Add(int a, int b) => a + b;
7
8    public string Describe()
9    {
10        return "This object is enhanced by the [Describe] aspect.";
11    }
12}
13

See also