Skip to content

Instantly share code, notes, and snippets.

@lomholdt
Last active January 4, 2022 20:19
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save lomholdt/0c36cf22fa4af5076aee0907e2b10a65 to your computer and use it in GitHub Desktop.
Save lomholdt/0c36cf22fa4af5076aee0907e2b10a65 to your computer and use it in GitHub Desktop.
Make internals visible to other projects (eg. test projects)
<Project>
<Target Name="InternalsVisibleToTask" BeforeTargets="GenerateAdditionalSources" Condition="@(InternalsVisibleTo) != ''">
<ItemGroup>
<AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
<_Parameter1>%(InternalsVisibleTo.Identity)</_Parameter1>
</AssemblyAttribute>
</ItemGroup>
</Target>
</Project>
@lomholdt
Copy link
Author

lomholdt commented Oct 5, 2020

Usage

Add a Directory.Build.props to your src folder with the above build task.

In the .csproj file containing the internal class add a

  <ItemGroup>
    <InternalsVisibleTo Include="Project.Name.UnitTest" />
  </ItemGroup>

@jonas-lomholdt
Copy link

As of .NET 5, this is natively supported
https://jolo.codes/blog/make-internals-visible-to-test-projects/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment