ufcpp/UfcppSample

Interceptors

Opened this issue · 2 comments

C# 12 時点で preview feature。オプション指定必須。

ufcpp-live/UfcppLiveAgenda#74 (comment)

using System.Runtime.CompilerServices;

var c = new C();
c.InterceptableMethod(1); // (L1,C1): prints "interceptor 1"
c.InterceptableMethod(1); // (L2,C2): prints "other interceptor 1"
c.InterceptableMethod(2); // (L3,C3): prints "other interceptor 2"
c.InterceptableMethod(1); // prints "interceptable 1"

class C
{
    public void InterceptableMethod(int param)
    {
        Console.WriteLine($"interceptable {param}");
    }
}

// generated code
static class D
{
    [InterceptsLocation(@"[full path to Program]\Program.cs", 4, 3) ]
    public static void InterceptorMethod(this C c, int param)
    {
        Console.WriteLine($"interceptor {param}");
    }

    [InterceptsLocation(@"[full path to Program]\Program.cs", 5, 3)]
    [InterceptsLocation(@"[full path to Program]\Program.cs", 6, 3)]
    public static void OtherInterceptorMethod(this C c, int param)
    {
        Console.WriteLine($"other interceptor {param}");
    }
}

https://ufcpp.net/blog/2024/2/interceptors/ ブログにはした。
プレビューの間はこれでいいや。

https://ufcpp.net/blog/2024/2/interceptors/

実際、パスを使うのはやめるっぽい。
location specifier を使う路線になった。