Open sandboxFocusImprove this doc

LAMA0503: The introduced member has a different type than the existing member

Severity Error
Reported by Metalama.Framework
Message The aspect '<AspectType>' cannot introduce member '<Member>' into type '<TargetType>' because it is already defined in type '<DeclaringType>' and has a different type or return type '<ReturnType>'.

Cause

When an aspect introduces a member that already exists in the target type or in a base type, the introduced member and the existing member must have compatible types. Metalama reports LAMA0503 in the following situations:

  • A method is introduced with OverrideStrategy.Override, and its return type can't be converted to the return type of the existing method by a reference conversion.
  • A property or an indexer is introduced, whatever the override strategy, and its type can't be converted to the type of the existing member by a reference conversion.
  • An event is introduced, whatever the override strategy, and its type isn't identical to the type of the existing event.

In the message, <DeclaringType> is the type that defines the existing member, and <ReturnType> is the type or return type of the existing member.

In the following example, the Versioned aspect overrides the GetVersion method of the base class Document. The template returns int, but the base method returns long.

1using Metalama.Framework.Aspects;
2using System;
3
4namespace Doc.LAMA0503.Error;
5
6public class VersionedAttribute : TypeAspect
7{
8    // The template returns int, but Document.GetVersion returns long.
9    [Introduce( WhenExists = OverrideStrategy.Override )]
10    public int GetVersion()
11    {
12        Console.WriteLine( "Getting the version." );
13
14        return meta.Proceed();
15    }
16}
17
1namespace Doc.LAMA0503.Error;
2
3internal class Document
4{
5    public virtual long GetVersion() => 1;
6}
7
8// Error: the introduced GetVersion method returns int, but the base method returns long.
9[Versioned]
Error LAMA0503: The aspect 'Versioned' cannot introduce member 'Contract.GetVersion()' into type 'Contract' because it is already defined in type 'Document' and has a different type or return type 'long'.

10internal partial class Contract : Document { }
11

Resolution

Depending on your intent, do one of the following:

  • Change the type or return type of the template so that it matches the existing member.
  • If the existing member isn't meant to be overridden, rename the introduced member or the existing member to remove the conflict.

In the following example, the template returns long, like the base method.

1using Metalama.Framework.Aspects;
2using System;
3
4namespace Doc.LAMA0503.Fixed;
5
6public class VersionedAttribute : TypeAspect
7{
8    // The template returns long, like Document.GetVersion.
9    [Introduce( WhenExists = OverrideStrategy.Override )]
10    public long GetVersion()
11    {
12        Console.WriteLine( "Getting the version." );
13
14        return meta.Proceed();
15    }
16}
17
Source Code
1namespace Doc.LAMA0503.Fixed;
2


3internal class Document
4{
5    public virtual long GetVersion() => 1;
6}
7
8// Fixed: the introduced GetVersion method returns long, like the base method.
9[Versioned]
10internal partial class Contract : Document { }
11
Transformed Code
1using System;
2
3namespace Doc.LAMA0503.Fixed;
4
5internal class Document
6{
7    public virtual long GetVersion() => 1;
8}
9
10// Fixed: the introduced GetVersion method returns long, like the base method.
11[Versioned]
12internal partial class Contract : Document
13{
14    public override long GetVersion()
15    {
16        Console.WriteLine("Getting the version.");
17        return base.GetVersion();
18    }
19}
20

See also