Tuesday Coding Tips 07 — Exposing internal bits of .NET projects

posted Originally published at medium.com 1 min read

Tuesday coding tips are super short posts about various tidbits mainly from C++, but also from other programming languages I use. You can also follow the #TuesdayCodingTips hashtag on Mastodon and Linkedin.


C# has a number of visibility keywords and their combinations. A peculiar one is ‘internal’ which restricts the visibility of the symbols to the current assembly. However, that can be tweaked.

You can manually specify the names of assemblies to which the ‘internal’ bits will be visible. Why is this useful? Because you can expose otherwise private parts of your code to assemblies like unit tests and test code with ease. No more ‘friend’ shenanigans like in C++ unit tests!

Ways how to configure this evolved over the years and the most up-to-date approach seems to be in configuring the project file directly (with the latest MSVC and .NET 5+).

<Project>
    <!-- ... --->

 <ItemGroup>
  <AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
   <_Parameter1>$(AssemblyName).Tests</_Parameter1>
  </AssemblyAttribute>
 </ItemGroup>

</Project>

More Posts

Tuesday Coding Tip 06 - Explicit template instantiation

Jakub Neruda - Apr 7

Tuesday Coding Tip 02 - Template with type-specific API

Jakub Neruda - Mar 10

Tuesday Coding Tip 05 - Object initialization in C++

Jakub Neruda - Mar 31

Tuesday Coding Tip 04 - Precompiled headers

Jakub Neruda - Mar 24

Tuesday Coding Tip 03 - Structured bindings

Jakub Neruda - Mar 17
chevron_left

Related Jobs

View all jobs →

Commenters (This Week)

7 comments
2 comments
2 comments

Contribute meaningful comments to climb the leaderboard and earn badges!