Open sandboxFocusImprove this doc

LAMA0527: An introduced indexer can't be static

Severity Error
Reported by Metalama.Framework
Message The aspect '<AspectType>' cannot introduce the indexer '<Member>' into the type '<TargetType>' because the indexer is static. C# does not support static indexers.

Cause

C# doesn't support static indexers: an indexer is always an instance member. Metalama reports LAMA0527 when an aspect introduces an indexer with the IntroduceIndexer method and the indexer is static. This happens in the following situations:

  • The aspect passes IntroductionScope.Static as the scope argument.
  • The buildIndexer delegate sets IsStatic to true.
  • The aspect uses the default scope and the accessor templates are static methods.

The error is reported on the target type.

In the following example, the Indexer aspect introduces the indexer with the static scope.

1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3using System;
4
5namespace Doc.LAMA0527.Error;
6
7public class IndexerAttribute : TypeAspect
8{
9    public override void BuildAspect( IAspectBuilder<INamedType> builder )
10    {
11        builder.IntroduceIndexer(
12            new[] { ( typeof(int), "index" ) },
13            nameof(this.GetItem),
14            nameof(this.SetItem),
15            // Error: indexers cannot be static.
16            scope: IntroductionScope.Static,
17            buildIndexer: indexer => { indexer.Accessibility = Accessibility.Public; } );
18    }
19
20    [Template]
21    public dynamic? GetItem()
22    {
23        Console.WriteLine( "Getting an item." );
24
25        return meta.Proceed();
26    }
27
28    [Template]
29    public void SetItem( dynamic? value )
30    {
31        Console.WriteLine( "Setting an item." );
32        meta.Proceed();
33    }
34}
35
1namespace Doc.LAMA0527.Error;
2
3// Error: the [Indexer] aspect introduces a static indexer.
4[Indexer]
Error LAMA0527: The aspect 'Indexer' cannot introduce the indexer 'ProductCatalog.this[int]' into the type 'ProductCatalog' because the indexer is static. C# does not support static indexers.

5internal partial class ProductCatalog { }
6

Resolution

Introduce an instance indexer:

  • Remove the scope argument, or pass IntroductionScope.Instance, and make sure that the accessor templates are instance methods.
  • Don't set IsStatic to true in the buildIndexer delegate.
  • If the target type is a static class, which can't contain an indexer, introduce a static method instead.

In the following example, the aspect uses the default scope, so the indexer is an instance member.

1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3using System;
4
5namespace Doc.LAMA0527.Fixed;
6
7public class IndexerAttribute : TypeAspect
8{
9    public override void BuildAspect( IAspectBuilder<INamedType> builder )
10    {
11        builder.IntroduceIndexer(
12            new[] { ( typeof(int), "index" ) },
13            nameof(this.GetItem),
14            nameof(this.SetItem),
15            // Fixed: the default scope introduces an instance indexer.
16            buildIndexer: indexer => { indexer.Accessibility = Accessibility.Public; } );
17    }
18
19    [Template]
20    public dynamic? GetItem()
21    {
22        Console.WriteLine( "Getting an item." );
23
24        return meta.Proceed();
25    }
26
27    [Template]
28    public void SetItem( dynamic? value )
29    {
30        Console.WriteLine( "Setting an item." );
31        meta.Proceed();
32    }
33}
34
Source Code
1namespace Doc.LAMA0527.Fixed;
2


3// Fixed: the [Indexer] aspect introduces an instance indexer.
4[Indexer]
5internal partial class ProductCatalog { }
6
Transformed Code
1using System;
2
3namespace Doc.LAMA0527.Fixed;
4
5// Fixed: the [Indexer] aspect introduces an instance indexer.
6[Indexer]
7internal partial class ProductCatalog
8{
9    public object? this[int index]
10    {
11        get
12        {
13            Console.WriteLine("Getting an item.");
14            return default(object?);
15        }
16        set
17        {
18            Console.WriteLine("Setting an item.");
19        }
20    }
21}
22

See also