Open sandboxFocusImprove this doc

LAMA0506: The introduced member conflicts with a member of a different kind

Severity Error
Reported by Metalama.Framework
Message The aspect '<AspectType>' cannot introduce member '<Member>' into type '<TargetType>' because there is already a <DeclarationKind> of the same name declared in the type or in a base type.

Cause

When an aspect introduces a member, Metalama looks for an existing member with the same name in the target type and in its base types. If it finds a member of the same kind, the WhenExists property, or the whenExists parameter of the advice method, decides what happens: the introduction can fail, be ignored, override the existing member, or hide it.

However, Metalama can't override or hide a member of a different kind. For instance, a property can't override a field, and a method can't share its name with a property. Metalama reports LAMA0506 in this situation, whatever the value of WhenExists. In the message, <DeclarationKind> is the kind of the existing member, for example field or property.

In the following example, the Versioned aspect introduces a Version property, but the Document class already declares a Version field.

1using Metalama.Framework.Aspects;
2
3namespace Doc.LAMA0506.Error;
4
5public class VersionedAttribute : TypeAspect
6{
7    // The aspect introduces a property named Version.
8    [Introduce]
9    public int Version { get; set; }
10}
11
1namespace Doc.LAMA0506.Error;
2
3// Error: the [Versioned] aspect introduces a Version property, but the class already has a Version field.
4[Versioned]
Error LAMA0506: The aspect 'Versioned' cannot introduce member 'Document.Version' into type 'Document' because there is already a field of the same name declared in the type or in a base type.

5public partial class Document
6{
7    public int Version = 1;
8
9    public string? Title { get; set; }
10}
11

Resolution

Depending on your intent, do one of the following:

  • Remove or rename the existing member if the aspect is meant to provide it. This is the fix shown in the example below.
  • Rename the introduced member. For declarative advice, set the Name property of the custom attribute.
  • Change the kind of the introduced member so that it matches the existing member, then set WhenExists to choose how the aspect handles the existing member.

In the following example, the class no longer declares its own Version field, so the aspect can introduce the property.

1using Metalama.Framework.Aspects;
2
3namespace Doc.LAMA0506.Fixed;
4
5public class VersionedAttribute : TypeAspect
6{
7    // The aspect introduces a property named Version.
8    [Introduce]
9    public int Version { get; set; }
10}
11
Source Code
1namespace Doc.LAMA0506.Fixed;
2

3// Fixed: the class no longer declares its own Version field, so the aspect can introduce the property.
4[Versioned]
5public partial class Document
6{
7    public string? Title { get; set; }
8}
9
Transformed Code
1namespace Doc.LAMA0506.Fixed;
2
3// Fixed: the class no longer declares its own Version field, so the aspect can introduce the property.
4[Versioned]
5public partial class Document
6{
7    public string? Title { get; set; }
8
9
10    public int Version { get; set; }
11}
12

See also