Declaring InternalsVisibleTo in a csproj file
A quick overview of how to declare InternalsVisibleTo in a csproj file.

Just a quick post today. More for my own personal reference (as I forget the syntax every time I try to do this), but it may be useful for others to read.

Back in the day, we used to declare the InternalsVisibleTo attribute in the AssemblyInfo.cs file. However, this file no longer exists in the new SDK-style projects. You can, of course, still make your own file and add the standard [assembly: InternalsVisibleTo("YourAssemblyName")] declaration to it.

But, there is also the option of putting this in the csproj itself, via the new AssemblyAttribute item:

<ItemGroup>
  <AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
    <_Parameter1>YourAssemblyName</_Parameter1>
  </AssemblyAttribute>
</ItemGroup>

As an aside, you can also use this method to add other custom attributes to your assembly.

Update for .NET 5 and above:

As of .NET 5.0, this gets even easier. You can just use the InternalsVisibleTo item directly!

  <ItemGroup>
    <InternalsVisibleTo Include="YourAssemblyName" />
  </ItemGroup>

Posted by Matthew King on 23 December 2021
Permission is granted to use all code snippets under CC BY-SA 3.0 (just like StackOverflow), or the MIT license - your choice!
If you enjoyed this post, and you want to show your appreciation, you can buy me a beverage on Ko-fi or Stripe