Open sandboxFocusImprove this doc

LAMA0115: A member isn't available for the current target

Severity Error
Reported by Metalama.Framework
Message The template '<Advice>' cannot use '<Expression>' when applied to the <TargetKind> '<TargetDeclaration>' because no '<MissingKind>' is available in this context.<AlternativeSuggestion>

Cause

The Target property gives a template access to the declaration being advised through members like Method, Property, Field, FieldOrProperty, Indexer, Parameter, Event, Constructor, or Type. Each of these members is available only when it makes sense for the current target. For instance, meta.Target.Field is available when the advice is applied to a field, but not when it's applied to a property.

Metalama reports LAMA0115 when it expands a template and the template reads a meta.Target member that isn't available for the current target. In the message, <Expression> is the member, such as meta.Target.Field, <TargetDeclaration> is the declaration being advised (for an accessor template, the accessor), and <MissingKind> is the kind of object that's missing. When a more general member exists, the message suggests it.

In the following example, the LogRead aspect reads meta.Target.Field.Name, but it's applied to the property Name.

1using Metalama.Framework.Aspects;
2using System;
3
4namespace Doc.LAMA0115.Error;
5
6public class LogReadAttribute : OverrideFieldOrPropertyAspect
7{
8    public override dynamic? OverrideProperty
9    {
10        get
11        {
12            // meta.Target.Field is available only when the target is a field.
13            Console.WriteLine( $"Reading {meta.Target.Field.Name}." );
14
15            return meta.Proceed();
16        }
17
18        set
19        {
20            meta.Proceed();
21        }
22    }
23}
24
1namespace Doc.LAMA0115.Error;
2
3internal class Customer
4{
5    // Error: the template of [LogRead] uses meta.Target.Field, but Name is a property.
6    [LogRead]
    Error LAMA0115: The template 'LogReadAttribute.OverrideProperty.get' cannot use 'meta.Target.Field' when applied to the method 'Customer.Name.get' because no 'IField' is available in this context. Consider using meta.Target.FieldOrProperty instead.

7    public string? Name { get; set; }
8}
9

Resolution

Use a member of meta.Target that's available for every declaration the aspect can be applied to:

In the following example, the template uses meta.Target.FieldOrProperty.

1using Metalama.Framework.Aspects;
2using System;
3
4namespace Doc.LAMA0115.Fixed;
5
6public class LogReadAttribute : OverrideFieldOrPropertyAspect
7{
8    public override dynamic? OverrideProperty
9    {
10        get
11        {
12            // meta.Target.FieldOrProperty is available for both fields and properties.
13            Console.WriteLine( $"Reading {meta.Target.FieldOrProperty.Name}." );
14
15            return meta.Proceed();
16        }
17
18        set
19        {
20            meta.Proceed();
21        }
22    }
23}
24
Source Code
1namespace Doc.LAMA0115.Fixed;
2


3internal class Customer
4{
5    // Fixed: the template of [LogRead] uses meta.Target.FieldOrProperty.
6    [LogRead]


7    public string? Name { get; set; }
8}
9
Transformed Code
1using System;
2
3namespace Doc.LAMA0115.Fixed;
4
5internal class Customer
6{
7    private string? _name;
8
9    // Fixed: the template of [LogRead] uses meta.Target.FieldOrProperty.
10    [LogRead]
11    public string? Name
12    {
13        get
14        {
15            Console.WriteLine("Reading Name.");
16            return _name;
17        }
18
19        set
20        {
21            _name = value;
22        }
23    }
24}
25

See also