Open sandboxFocusImprove this doc

LAMA0543: An extension block can't contain a member with this modifier

Severity Error
Reported by Metalama.Framework
Message The aspect '<Aspect>' cannot introduce '<Member>' into '<ExtensionBlock>' because the member is '<Modifier>', which the language forbids for an extension member.

Cause

An extension member isn't a member of the extended type, so the C# language forbids the modifiers that make sense only in a class hierarchy. The forbidden modifiers are abstract, virtual, override, new, sealed, partial and protected.

Metalama reports LAMA0543 when an aspect calls IntroduceIndexer on an extension block and the indexer has one of these modifiers. <Modifier> in the message names the modifier. Metalama also checks the accessors: an indexer with a protected get or set accessor is rejected even if the indexer itself is public.

In the following example, the template of the set accessor is protected, so the aspect introduces an indexer with a protected set accessor.

1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3using System;
4using System.Linq;
5
6namespace Doc.LAMA0543.Error;
7
8public class ItemIndexerAttribute : TypeAspect
9{
10    public override void BuildAspect( IAspectBuilder<INamedType> builder )
11    {
12        var extensionBlock = builder.With( builder.Target.ExtensionBlocks.Single() );
13
14        extensionBlock.IntroduceIndexer( typeof(int), nameof(this.GetItem), nameof(this.SetItem) );
15    }
16
17    [Template]
18    public string GetItem( int index )
19    {
20        Console.WriteLine( "Get." );
21
22        return index.ToString();
23    }
24
25    // Error: the set accessor is protected, which an extension member does not allow.
26    [Template]
27    protected void SetItem( int index, string value )
28    {
29        Console.WriteLine( "Set." );
30    }
31}
32
1namespace Doc.LAMA0543.Error;
2
3[ItemIndexer]
Error LAMA0543: The aspect 'ItemIndexer' cannot introduce 'IntExtensions.extension(int).this[int]' into 'IntExtensions.extension(int)' because the member is 'protected', which the language forbids for an extension member.

4internal static class IntExtensions
5{
6    extension( int number )
7    {
8    }
9}
10

Resolution

Remove the modifier named in the message, or give the accessor the same accessibility as the indexer. In the following example, the template of the set accessor is public.

1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3using System;
4using System.Linq;
5
6namespace Doc.LAMA0543.Fixed;
7
8public class ItemIndexerAttribute : TypeAspect
9{
10    public override void BuildAspect( IAspectBuilder<INamedType> builder )
11    {
12        var extensionBlock = builder.With( builder.Target.ExtensionBlocks.Single() );
13
14        extensionBlock.IntroduceIndexer( typeof(int), nameof(this.GetItem), nameof(this.SetItem) );
15    }
16
17    [Template]
18    public string GetItem( int index )
19    {
20        Console.WriteLine( "Get." );
21
22        return index.ToString();
23    }
24
25    // The set accessor is public.
26    [Template]
27    public void SetItem( int index, string value )
28    {
29        Console.WriteLine( "Set." );
30    }
31}
32
Source Code
1namespace Doc.LAMA0543.Fixed;
2
3[ItemIndexer]


4internal static class IntExtensions
5{
6    extension( int number )
7    {
8    }






9}
10
Transformed Code
1using System;
2
3namespace Doc.LAMA0543.Fixed;
4
5[ItemIndexer]
6internal static class IntExtensions
7{
8    extension(int number)
9    {
10        public string this[int index]
11        {
12            get
13            {
14                Console.WriteLine("Get.");
15                return index.ToString();
16            }
17            set
18            {
19                Console.WriteLine("Set.");
20            }
21        }
22    }
23}
24

See also