| Severity | Error |
| Reported by | Metalama.Framework |
| Message | The aspect '<AspectType>' cannot introduce the partial member '<Member>' into the type '<TargetType>' because the type is not partial. Add the partial modifier to the type. |
Cause
An aspect introduces a partial member when the template sets the IsPartial property to true. Partial members are typically extension points: the aspect declares a partial method, and you can implement it in your own part of the class.
In C#, only a partial type can contain partial members. Metalama reports LAMA0535 when the target type of the introduction isn't declared with the partial modifier.
In the following example, the InitializationHook aspect introduces a partial OnInitialized method into a class that isn't partial.
1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3
4namespace Doc.LAMA0535.Error;
5
6public class InitializationHookAttribute : TypeAspect
7{
8 public override void BuildAspect( IAspectBuilder<INamedType> builder )
9 {
10 builder.IntroduceMethod( nameof(this.OnInitialized) );
11 }
12
13 // Introduces a partial method that the user can implement in another part of the class.
14#pragma warning disable CS0626
15 [Template( IsPartial = true )]
16 private extern void OnInitialized();
17#pragma warning restore CS0626
18}
19
1namespace Doc.LAMA0535.Error;
2
3// Error: the class isn't partial, so the aspect can't introduce a partial method into it.
4[InitializationHook]
Error LAMA0535: The aspect 'InitializationHook' cannot introduce the partial member 'OrderService.OnInitialized()' into the type 'OrderService' because the type is not partial. Add the partial modifier to the type.
5internal class OrderService
6{
7 public void PlaceOrder( int orderId ) { }
8}
9
Resolution
Add the partial modifier to the target type, as shown in the following example. It's good practice to mark all classes that receive introduced members as partial anyway, because it lets the IDE see the introduced members at design time.
If you're the author of the aspect and the member doesn't need to be partial, remove IsPartial = true from the template.
1using Metalama.Framework.Aspects;
2using Metalama.Framework.Code;
3
4namespace Doc.LAMA0535.Fixed;
5
6public class InitializationHookAttribute : TypeAspect
7{
8 public override void BuildAspect( IAspectBuilder<INamedType> builder )
9 {
10 builder.IntroduceMethod( nameof(this.OnInitialized) );
11 }
12
13 // Introduces a partial method that the user can implement in another part of the class.
14#pragma warning disable CS0626
15 [Template( IsPartial = true )]
16 private extern void OnInitialized();
17#pragma warning restore CS0626
18}
19
1namespace Doc.LAMA0535.Fixed;
2
3// Fixed: the class is partial.
4[InitializationHook]
5internal partial class OrderService
6{
7 public void PlaceOrder( int orderId ) { }
8}
9
1namespace Doc.LAMA0535.Fixed;
2
3// Fixed: the class is partial.
4[InitializationHook]
5internal partial class OrderService
6{
7 public void PlaceOrder(int orderId) { }
8
9 partial void OnInitialized();
10}
11