Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Interceptors #456

Open
Tracked by #462 ...
ufcpp opened this issue Oct 17, 2023 · 7 comments
Open
Tracked by #462 ...

Interceptors #456

ufcpp opened this issue Oct 17, 2023 · 7 comments
Labels

Comments

@ufcpp
Copy link
Owner

ufcpp commented Oct 17, 2023

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}");
    }
}
namespace System.Runtime.CompilerServices;

[Diagnostics.Conditional("CompileTimeOnly")]
[AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
internal sealed class InterceptsLocationAttribute : Attribute
{
    public InterceptsLocationAttribute(string path, int lineNumber, int columnNumber)
    {
        _ = path;
        _ = lineNumber;
        _ = columnNumber;
    }
}
@ufcpp ufcpp mentioned this issue Oct 17, 2023
9 tasks
@ufcpp
Copy link
Owner Author

ufcpp commented Feb 18, 2024

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

@ufcpp ufcpp added the C# 13.0 label Feb 18, 2024
@ufcpp
Copy link
Owner Author

ufcpp commented May 29, 2024

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

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

@ufcpp ufcpp mentioned this issue Jul 14, 2024
15 tasks
@ufcpp
Copy link
Owner Author

ufcpp commented Sep 3, 2024

location specifier 使う路線、いまだ Experimental だった。
なんか、C# 13 でも正式リリースに至らないんじゃないかという気がしてきてる。

using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;

var code = /* lang=c#-test */"""
var x = int.Parse("123");

""";

var tree = CSharpSyntaxTree.ParseText(code, path: "a.cs");

var compilation = CSharpCompilation.Create("", [tree]);
var sem = compilation.GetSemanticModel(tree);

var root = await tree.GetRootAsync();
var parse = root.FindNode(new(13, 1));

var x = (InvocationExpressionSyntax)parse.Parent.Parent;

#pragma warning disable RSEXPERIMENTAL002
var loc = sem.GetInterceptableLocation(x);
#pragma warning restore RSEXPERIMENTAL002

Console.WriteLine(loc.Version);
Console.WriteLine(loc.Data);

var decoded = Convert.FromBase64String(loc.Data);

foreach (var b in decoded)
{
    Console.Write($"{b:X2} ");
}

Console.WriteLine();

@ufcpp
Copy link
Owner Author

ufcpp commented Sep 4, 2024

だいぶ前からだけども、global な型には Interceptor メソッド定義できない。

@ufcpp
Copy link
Owner Author

ufcpp commented Oct 31, 2024

C# 13 で、<LangVersion>preview</LangVersion><Features>InterceptorsPreview</Features> も不要にはなってる。
初期に InterceptorsPreviewNamespaces だったものも、今は InterceptorsNamespaces。

InterceptsLocationAttribute(string filePath, int line, int column) なやつは preview なしで動いてそう。

InterceptsLocationAttribute(string locationSpecifier) なやつは…
Roslyn 4.12 でもまだ semanticModel.GetInterceptableLocation(node); に RSEXPERIMENTAL002 が出る。

@ufcpp
Copy link
Owner Author

ufcpp commented Nov 12, 2024

プレースホルダーだけ先に:
https://ufcpp.net/study/csharp/cheatsheet/ap_ver13/#interceptor

@ufcpp
Copy link
Owner Author

ufcpp commented Nov 14, 2024

↓これを例に書くかなぁ。

https://gist.github.com/ufcpp/ae6188c21da203c71eab55037c1ef13f

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant