Open sandboxFocusImprove this doc

LAMA0504: The introduced member and the existing member differ in staticity

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 one of the two members is static while the other is not.

Cause

When an aspect introduces a method, property, field or event, Metalama checks whether the target type, or one of its visible base types, already defines a member with the same name, or the same signature for methods. If such a member exists, both members must be static, or both must be instance members.

Metalama reports LAMA0504 when one of the two members is static and the other isn't. This check runs before the override strategy is considered, so changing the strategy doesn't remove the error.

In the following example, the Describable aspect introduces an instance method Describe(), but the Product class already has a static method Describe().

1using Metalama.Framework.Aspects;
2
3namespace Doc.LAMA0504.Error;
4
5public class DescribableAttribute : TypeAspect
6{
7    [Introduce]
8    public string Describe()
9    {
10        return $"An instance of {meta.Target.Type.Name}.";
11    }
12}
13
1namespace Doc.LAMA0504.Error;
2
3[Describable]
Error LAMA0504: The aspect 'Describable' cannot introduce member 'Product.Describe()' into type 'Product' because it is already defined in type 'Product', and one of the two members is static while the other is not.

4internal partial class Product
5{
6    // Error: this static method has the same signature as the instance method introduced by the aspect.
7    public static string Describe() => "A product of the catalog.";
8}
9

Resolution

Depending on your intent, do one of the following:

  • Rename the existing member or the introduced member, so that they no longer conflict.
  • If both members represent the same thing, make the template and the existing member both static or both instance members, and choose an override strategy such as OverrideStrategy.Override or OverrideStrategy.Ignore.

In the following example, the static method is renamed DescribeType.

1using Metalama.Framework.Aspects;
2
3namespace Doc.LAMA0504.Fixed;
4
5public class DescribableAttribute : TypeAspect
6{
7    [Introduce]
8    public string Describe()
9    {
10        return $"An instance of {meta.Target.Type.Name}.";
11    }
12}
13
Source Code
1namespace Doc.LAMA0504.Fixed;
2

3[Describable]
4internal partial class Product
5{
6    // Fixed: the static method has a different name, so it no longer conflicts with the introduced method.
7    public static string DescribeType() => "A product of the catalog.";
8}
9
Transformed Code
1namespace Doc.LAMA0504.Fixed;
2
3[Describable]
4internal partial class Product
5{
6    // Fixed: the static method has a different name, so it no longer conflicts with the introduced method.
7    public static string DescribeType() => "A product of the catalog.";
8
9    public string Describe()
10    {
11        return "An instance of Product.";
12    }
13}
14

See also