.NET 8 - C#12 - 预览尝试拦截器

问题描述 投票:0回答:3

我想尝试新的 C# 12 预览功能 Interceptors 但似乎无法让它们工作。

页面说

预览功能在 Visual Studio 17.7 预览版 3 中引入。

所以我尝试使用 Jetbrains Rider。因为我看过 Nick Chapsas Youtube 视频

我的项目文件如下所示:

<Project Sdk="Microsoft.NET.Sdk">
    <PropertyGroup>
        <OutputType>Exe</OutputType>
        <TargetFramework>net8.0</TargetFramework>
        <ImplicitUsings>enable</ImplicitUsings>
        <Nullable>enable</Nullable>
        <LangVersion>preview</LangVersion>
        <Features>InterceptorsPreview</Features>
    </PropertyGroup>
</Project>

我使用了此页面

中的示例代码

我又看了一些 YouTube 视频,发现

InterceptsLocationAttribute
位于他的一个帮助文件中,因此我尝试将该属性的代码添加到我的代码项目中。

代码编译总线仍然无法工作。代码执行没有跳转到拦截代码

c# interceptor .net-8.0
3个回答
2
投票

更新: 现在 Visual Studio 17.7 预览版 3 已经发布,它可以按照 Visual Studio 预览版的预期工作

我让它工作了,更改已经在 https://github.com/dotnet/roslyn 的主分支中,因此通过在本地构建,我可以启动新版本的 Visual Studio 并在其中运行代码。 这使它起作用,所以我认为尼克也做了同样的事情。


0
投票

是的,C#12 编译器无法识别 [InterceptsLocation] 属性,因为当前 System.Runtime.CompilerServices 中缺少该属性。

虽然它是 C#12 的提案,但目前在 System.Runtime.CompilerServices 中尚不可用。我们需要将其添加到 System.Runtime.CompilerServices 命名空间中,然后它就会按预期工作。

偶然发现一个 Youtube 频道“Learn N Njoy”,其中对此进行了讨论和解释。这是链接:

https://www.youtube.com/watch?v=u-BtY5QugbQ


0
投票

你怀念的是一个包含以下内容的辅助类:

namespace System.Runtime.CompilerServices
{
  [AttributeUsage(AttributeTargets.Method, AllowMultiple = true)]
  public sealed class InterceptsLocationAttribute(string filePath, int line, int character) : Attribute
  {
  }
}
© www.soinside.com 2019 - 2024. All rights reserved.