Open sandboxFocusImprove this doc

LAMA1008: The type has no method of the name selected for interception

Severity Warning
Reported by Metalama.Extensions.Interceptors
Message The <Source> intercepts methods named '<Name>' declared by '<Type>', but no such method exists.<Detail>

Cause

Metalama.Extensions.Interceptors lets an aspect or a fabric replace the calls to selected methods with calls to other methods. In a registration made with the InterceptMethods method, the Type method selects the declaring type, and the Methods method selects the intercepted methods by name.

Metalama reports LAMA1008 when the declaring type doesn't declare an ordinary method of one of these names. The registration then intercepts no call. The usual causes are a misspelled name, a method that the type inherits from its base type instead of declaring it, or a name that designates a property, an event, or a C# 14 extension member, whose accessors can't be intercepted in this version.

In the message, <Source> describes the aspect or fabric that made the registration, <Name> is the selected name, and <Type> is the declaring type. <Detail> is empty, or it's an additional sentence when the name designates a property, an event, or an extension member.

In the following example, the fabric selects the method Prnt, but the Printer class declares Print.

1using Metalama.Extensions.Interceptors;
2using Metalama.Framework.Code;
3using Metalama.Framework.Fabrics;
4using System;
5using System.Linq;
6
7namespace Doc.LAMA1008.Error;
8
Warning LAMA1008: The interceptor method 'Interceptors.Print(string)' registered by the fabric Doc.LAMA1008.Error.Fabric intercepts methods named 'Prnt' declared by 'Printer', but no such method exists.

9internal class Fabric : ProjectFabric
10{
11    public override void AmendProject( IProjectAmender amender )
12    {
13        amender.InterceptMethods(
14            b => b
15                .Type( typeof(Printer) )
16                .Methods( "Prnt" ) // Warning: the Printer class has no method named Prnt.
17                .RedirectToExistingMethod( typeof(Interceptors).AsINamedType().Methods.OfName( "Print" ).Single() ) );
18    }
19}
20
21internal static class Interceptors
22{
23    public static void Print( string value ) => Console.WriteLine( "intercepted: " + value );
24}
25
1using System;
2
3namespace Doc.LAMA1008.Error;
4
5internal static class Printer
6{
7    public static void Print( string value ) => Console.WriteLine( value );
8}
9
10internal static class Program
11{
12    public static void Run() => Printer.Print( "Hello" );
13}
14

Resolution

Depending on the cause, do one of the following:

  • Correct the name given to Methods. In the following example, the name matches the Print method.
  • Select the base type that declares the method, instead of the type that inherits it.
  • If the name designates a property, an event, or an extension member, remove the registration.

1using Metalama.Extensions.Interceptors;
2using Metalama.Framework.Code;
3using Metalama.Framework.Fabrics;
4using System;
5using System.Linq;
6
7namespace Doc.LAMA1008.Fixed;
8
9internal class Fabric : ProjectFabric
10{
11    public override void AmendProject( IProjectAmender amender )
12    {
13        amender.InterceptMethods(
14            b => b
15                .Type( typeof(Printer) )
16                .Methods( "Print" ) // The name matches the Print method of the Printer class.
17                .RedirectToExistingMethod( typeof(Interceptors).AsINamedType().Methods.OfName( "Print" ).Single() ) );
18    }
19}
20
21internal static class Interceptors
22{
23    public static void Print( string value ) => Console.WriteLine( "intercepted: " + value );
24}
25
Source Code
1using System;
2
3namespace Doc.LAMA1008.Fixed;
4
5internal static class Printer
6{
7    public static void Print( string value ) => Console.WriteLine( value );
8}
9
10internal static class Program
11{
12    public static void Run() => Printer.Print( "Hello" );
13}
14
Transformed Code
1using System;
2
3namespace Doc.LAMA1008.Fixed;
4
5internal static class Printer
6{
7    public static void Print(string value) => Console.WriteLine(value);
8}
9
10internal static class Program
11{
12    public static void Run() => Interceptors.Print("Hello");
13}
14

See also