2

I would like to try the new C# 12 preview function Interceptors but can't seem to get them to work.

The page says

Preview feature Introduced in Visual Studio 17.7, preview 3.

So I tried using Jetbrains Rider. Because I have seen Nick Chapsas Youtube video

My project file looks like this:

<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>

I used the sample code from this page

I have looked at the Youtube video some more and saw that the InterceptsLocationAttribute was in one of his helper files so I tried adding the code for that attribute to my code project.

Than the code compiles bus still not works. The code execution did not jump to the interception code.

Guru Stron
  • 102,774
  • 10
  • 95
  • 132
Connor Stoop
  • 1,574
  • 1
  • 12
  • 26

2 Answers2

2

Update: Now that Visual Studio 17.7, preview 3 has been released, it works as intended in visual studio preview

I got it working the changes are already in the main branch of https://github.com/dotnet/roslyn so by building that locally i can start a new version of visual studio and run the code in that. That makes it work, so I think Nick did the same.

tymtam
  • 31,798
  • 8
  • 86
  • 126
Connor Stoop
  • 1,574
  • 1
  • 12
  • 26
  • There is a package being developed to use to use this feature to mock code in test: https://github.com/demonicmetalcs/IntercepTest – Connor Stoop Jul 25 '23 at 07:23
0

Yes, [InterceptsLocation] attribute was not recognized by C#12 compiler as it's missing in System.Runtime.CompilerServices currently.

Although it's in proposal for C#12 but somehow not available in System.Runtime.CompilerServices as of now. We need to add it in System.Runtime.CompilerServices namespace and then it works as expected.

Came across one Youtube Channel "Learn N Njoy" where it was discussed and explained. Here's the link:

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