| Severity | Error |
| Reported by | Metalama.Framework |
| Message | The aspect '<AspectType>' cannot introduce a member of kind '<IntroducedDeclarationKind>' because the target type is of kind '<TargetTypeKind>'. Members can be introduced only into a class, a struct, or an interface. |
Cause
When an aspect class contains members marked with the [Introduce] custom attribute, Metalama introduces these members into the target type of the aspect, or into the declaring type when the aspect is applied to a member. Only classes, structs, and interfaces, including records, can receive introduced members.
Metalama reports LAMA0750 when this type is of another kind, typically an enum or a delegate. This can happen because TypeAspect can be applied to enums and delegates, and because a fabric can add an aspect to all types of a project, enums included. The aspect is then skipped for this target. In the message, <AspectType> is the aspect, <IntroducedDeclarationKind> is the kind of the member to introduce, and <TargetTypeKind> is the kind of the target type, for instance Enum.
In the following example, the TypeName aspect introduces a method, but it's applied to an enum.
1using Metalama.Framework.Aspects;
2
3namespace Doc.LAMA0750.Error;
4
5public class TypeNameAttribute : TypeAspect
6{
7 [Introduce]
8 public string GetTypeName() => meta.Target.Type.Name;
9}
10
1namespace Doc.LAMA0750.Error;
2
3// Error: the aspect introduces a method, but an enum can't receive introduced members.
4[TypeName]
Error LAMA0750: The aspect 'TypeName' cannot introduce a member of kind 'method' because the target type is of kind 'Enum'. Members can be introduced only into a class, a struct, or an interface.
5public enum OrderStatus
6{
7 Pending,
8 Shipped,
9 Delivered
10}
11
12public class Order
13{
14 public OrderStatus Status { get; set; }
15}
16
Resolution
Do one of the following:
- Apply the aspect to a class, a struct, or an interface. In the following example, the aspect is moved from the enum to the class that uses it.
- When you add the aspect with a fabric, select only the types that can receive members, for instance by filtering on the TypeKind property of the types.
- If you're the author of the aspect, add an eligibility condition in its
BuildEligibilitymethod that rejects enums and delegates, so that the aspect can't be applied to them in the first place.
1using Metalama.Framework.Aspects;
2
3namespace Doc.LAMA0750.Fixed;
4
5public class TypeNameAttribute : TypeAspect
6{
7 [Introduce]
8 public string GetTypeName() => meta.Target.Type.Name;
9}
10
1namespace Doc.LAMA0750.Fixed;
2
3public enum OrderStatus
4{
5 Pending,
6 Shipped,
7 Delivered
8}
9
10// Fixed: the aspect is applied to a class, which can receive introduced members.
11[TypeName]
12public partial class Order
13{
14 public OrderStatus Status { get; set; }
15}
16
1namespace Doc.LAMA0750.Fixed;
2
3public enum OrderStatus
4{
5 Pending,
6 Shipped,
7 Delivered
8}
9
10// Fixed: the aspect is applied to a class, which can receive introduced members.
11[TypeName]
12public partial class Order
13{
14 public OrderStatus Status { get; set; }
15
16 public string GetTypeName()
17 {
18 return "Order";
19 }
20}
21