Open sandboxFocusImprove this doc

LAMA0252: A template can't be partial

Severity Error
Reported by Metalama.Framework
Message '<Declaration>' cannot be partial because it is a template.

Cause

A template is a member of an aspect that Metalama compiles into code-generating code, for instance a method marked with [Template] or [Introduce], or an override of OverrideMethod. Metalama doesn't support partial templates.

Metalama reports LAMA0252 on the partial keyword of a method, property, or indexer that's a template. When the template has a defining declaration and an implementing declaration, Metalama reports the error on both parts. The aspect class itself can be partial; only its template members can't.

In the following example, the Describe method introduced by the aspect is declared in one part of the aspect class and implemented in another part.

1using Metalama.Framework.Aspects;
2using System;
3
4namespace Doc.LAMA0252.Error;
5
6public partial class DescribeAttribute : TypeAspect
7{
8    // Error: the introduced method is a template, so it cannot be partial.
9    [Introduce]
    Error LAMA0252: 'DescribeAttribute.Describe()' cannot be partial because it is a template.

10    public partial void Describe();
11}
12
13public partial class DescribeAttribute
14{
    Error LAMA0252: 'DescribeAttribute.Describe()' cannot be partial because it is a template.

15    public partial void Describe()
16    {
17        Console.WriteLine( $"This is a {meta.Target.Type.Name}." );
18    }
19}
20
1namespace Doc.LAMA0252.Error;
2
3[Describe]
4internal partial class Customer
5{
6    public string? Name { get; set; }
7}
8

Resolution

Remove the partial modifier and merge the declaration and the implementation of the template into a single member, as in the following example. You can still split the aspect class into several partial parts, as long as each template is entirely declared in one of them.

1using Metalama.Framework.Aspects;
2using System;
3
4namespace Doc.LAMA0252.Fixed;
5
6public class DescribeAttribute : TypeAspect
7{
8    // Fixed: the template is declared and implemented in a single, non-partial method.
9    [Introduce]
10    public void Describe()
11    {
12        Console.WriteLine( $"This is a {meta.Target.Type.Name}." );
13    }
14}
15
Source Code
1namespace Doc.LAMA0252.Fixed;
2


3[Describe]
4internal partial class Customer
5{
6    public string? Name { get; set; }
7}
8
Transformed Code
1using System;
2
3namespace Doc.LAMA0252.Fixed;
4
5[Describe]
6internal partial class Customer
7{
8    public string? Name { get; set; }
9
10    public void Describe()
11    {
12        Console.WriteLine("This is a Customer.");
13    }
14}
15

See also