Open sandboxFocusImprove this doc

LAMA0511: The [InterfaceMember] template doesn't have the type of the interface member

Severity Error
Reported by Metalama.Framework
Message The aspect '<AspectType>' cannot implement interface '<InterfaceType>' in the type '<TargetType>' because the aspect member '<DeclarativeIntroduction>', marked with the [InterfaceMember] attribute, does not have the same type or return type as the corresponding interface member '<InterfaceMember>'.

Cause

When an aspect calls ImplementInterface, Metalama looks in the aspect class for members marked with [InterfaceMember] that match each member of the interface by name and parameters. These members are the templates of the interface implementation.

Metalama reports LAMA0511 when such a template has the name and parameters of an interface member but not the same type: the return type of a method, or the type of a property or event. A different ref kind also triggers the error. The check applies to both implicit and explicit implementations. In the message, <DeclarativeIntroduction> is the aspect member and <InterfaceMember> is the interface member.

In the following example, the Describe template returns object, but IDescribable.Describe() returns string.

1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3
4namespace Doc.LAMA0511.Error;
5
6public interface IDescribable
7{
8    string Describe();
9}
10
11public class DescribableAttribute : TypeAspect
12{
13    public override void BuildAspect( IAspectBuilder<INamedType> builder )
14    {
15        builder.ImplementInterface( typeof(IDescribable) );
16    }
17
18    // The return type object doesn't match the return type string of IDescribable.Describe().
19    [InterfaceMember]
20    public object Describe()
21    {
22        return meta.Target.Type.Name;
23    }
24}
25
1namespace Doc.LAMA0511.Error;
2
3// Error: the Describe() template of the aspect doesn't have the return type of IDescribable.Describe().
4[Describable]
Error LAMA0511: The aspect 'Describable' cannot implement interface 'IDescribable' in the type 'Product' because the aspect member 'DescribableAttribute.Describe()', marked with the [InterfaceMember] attribute, does not have the same type or return type as the corresponding interface member 'IDescribable.Describe()'.

5internal partial class Product
6{
7    public decimal Price { get; set; }
8}
9

Resolution

Give the [InterfaceMember] template exactly the same type as the interface member. In the following example, the template returns string.

1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3
4namespace Doc.LAMA0511.Fixed;
5
6public interface IDescribable
7{
8    string Describe();
9}
10
11public class DescribableAttribute : TypeAspect
12{
13    public override void BuildAspect( IAspectBuilder<INamedType> builder )
14    {
15        builder.ImplementInterface( typeof(IDescribable) );
16    }
17
18    // The return type matches the return type of IDescribable.Describe().
19    [InterfaceMember]
20    public string Describe()
21    {
22        return meta.Target.Type.Name;
23    }
24}
25
Source Code
1namespace Doc.LAMA0511.Fixed;
2

3// Fixed: the Describe() template of the aspect has the same return type as IDescribable.Describe().
4[Describable]
5internal partial class Product
6{
7    public decimal Price { get; set; }
8}
9
Transformed Code
1namespace Doc.LAMA0511.Fixed;
2
3// Fixed: the Describe() template of the aspect has the same return type as IDescribable.Describe().
4[Describable]
5internal partial class Product : IDescribable
6{
7    public decimal Price { get; set; }
8
9    public string Describe()
10    {
11        return "Product";
12    }
13}
14

See also