How to Configure InternalsVisibleTo in the *.csproj File

As we moved from .Net Full Framework to .NET 6, we had to use the little hack of creating a AssemblyInfo.cs file and put this content into it to access the internal classes and methods from our test project:

1
2
3
using System.Runtime.CompilerServices;

[assembly:InternalsVisibleTo("Logic.Tests")]

In the meantime, Microsoft made some more changes, and now we can use this straightforward approach in our *.csproj files:

1
2
3
4
5
6
7
8
9
<Project Sdk="Microsoft.NET.Sdk">
  <PropertyGroup>
    <TargetFramework>net9.0</TargetFramework>
  </PropertyGroup>
  <ItemGroup>
    <InternalsVisibleTo Include="Logic.Tests" />
  </ItemGroup>

</Project>

We must modify the *.csproj file of the project with the internal classes and point it to the test project where we want to access the internals from. Sometimes we need to restart Visual Studio or reload the project, while on other occasions that change went without any delays.

I like this approach a lot better than the older way with the AssemblyInfo.cs file. Maybe this time Microsoft keeps everything as it is and does not introduce yet another approach.