Open sandboxFocusImprove this doc

LAMA0505: An aspect can't introduce an instance member into a static class

Severity Error
Reported by Metalama.Framework
Message The aspect '<AspectType>' cannot introduce instance member '<Member>' into type '<TargetType>' because the type is static.

Cause

In C#, a static class can only contain static members. Metalama reports LAMA0505 when an aspect introduces a member that isn't static into a static class.

Whether an introduced member is static depends on the introduction scope, set by the scope parameter of methods like IntroduceMethod or by the Scope property. With the default scope, the introduced member is static only when the template is static. Therefore, an aspect that uses an instance template and the default scope introduces an instance member, even when the target class is static.

In the following example, the Describe aspect introduces an instance method into the static MathHelper class.

1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3
4namespace Doc.LAMA0505.Error;
5
6public class DescribeAttribute : TypeAspect
7{
8    public override void BuildAspect( IAspectBuilder<INamedType> builder )
9    {
10        // The template is an instance method, so the introduced member is an instance member.
11        builder.IntroduceMethod( nameof(Describe) );
12    }
13
14    [Template]
15    public string Describe()
16    {
17        return meta.Target.Type.Name;
18    }
19}
20
1namespace Doc.LAMA0505.Error;
2
3// Error: the [Describe] aspect introduces an instance method into a static class.
4[Describe]
Error LAMA0505: The aspect 'Describe' cannot introduce instance member 'MathHelper.Describe()' into type 'MathHelper' because the type is static.

5internal static partial class MathHelper
6{
7    public static int Square( int x ) => x * x;
8}
9

Resolution

Depending on your intent, do one of the following:

  • Introduce a static member. Pass IntroductionScope.Static as the scope, or make the template static. If the aspect can be applied to both static and non-static classes, choose the scope according to the IsStatic property of the target type.
  • Make the target class non-static, if the introduced member really needs an instance.
  • If you're the author of the aspect and it doesn't support static classes, make the aspect ineligible for static types in its BuildEligibility method, so that users get a clearer error when they apply it to a static class.

In the following example, the aspect introduces the method as a static method.

1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3
4namespace Doc.LAMA0505.Fixed;
5
6public class DescribeAttribute : TypeAspect
7{
8    public override void BuildAspect( IAspectBuilder<INamedType> builder )
9    {
10        // The member is introduced as a static member.
11        builder.IntroduceMethod( nameof(Describe), scope: IntroductionScope.Static );
12    }
13
14    [Template]
15    public string Describe()
16    {
17        return meta.Target.Type.Name;
18    }
19}
20
Source Code
1namespace Doc.LAMA0505.Fixed;
2

3// Fixed: the [Describe] aspect introduces a static method.
4[Describe]
5internal static partial class MathHelper
6{
7    public static int Square( int x ) => x * x;
8}




9
Transformed Code
1namespace Doc.LAMA0505.Fixed;
2
3// Fixed: the [Describe] aspect introduces a static method.
4[Describe]
5internal static partial class MathHelper
6{
7    public static int Square(int x) => x * x;
8
9    public static string Describe()
10    {
11        return "MathHelper";
12    }
13}
14

See also