0 day streak
csharp / expert
Snippet

Internals Visibility for Test Assemblies

To maintain strong encapsulation, logic is often marked as 'internal'. The 'InternalsVisibleTo' attribute allows specific test assemblies to access these members without making them public to the entire world, facilitating deep unit testing.

snippet.cs
csharp
1
2
3
4
5
6
7
8
9
// In AssemblyInfo.cs or anywhere at the top level
using System.Runtime.CompilerServices;
[assembly: InternalsVisibleTo("UnitTestsProject")]
internal class CoreLogic
{
internal int CalculateHiddenMetric() => 42;
}
Breakdown
1
[assembly: InternalsVisibleTo(...)]
An assembly-level attribute that grants trust to a named external project.
2
internal class CoreLogic
Restricts visibility to the defining assembly normally, but overridden by the attribute above.