ufcpp / UfcppSample

http://ufcpp.net/ 向けのサンプル
Apache License 2.0
133 stars 41 forks source link

Interceptors #456

Open ufcpp opened 8 months ago

ufcpp commented 8 months ago

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

https://github.com/ufcpp-live/UfcppLiveAgenda/issues/74#issuecomment-1633665906

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}");
    }
}
ufcpp commented 4 months ago

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

ufcpp commented 1 month ago

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

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