Skip to content

Friend assemblies C# 2.0type safety

Use `InternalsVisibleTo` to expose `internal` members to specific assemblies.

The internal access modifier keeps types and members visible only within the same assembly. This is great for encapsulation but makes it difficult to write unit tests for internal types without making them public.

C# 2.0 adds the InternalsVisibleTo assembly attribute which allows you to designate specific "friend" assemblies that can access internal members as if they were in the same assembly.

Code ​

csharp
// In AssemblyInfo.cs or a top-level file
using System.Runtime.CompilerServices;

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

// In the main project
internal class OrderProcessor
{
    internal bool Validate(Order order) { ... }
}

// In MyProject.Tests - can access internal members directly
var processor = new OrderProcessor();
Assert.True(processor.Validate(testOrder));
csharp
// Must make everything public to test it
public class OrderProcessor
{
    public bool Validate(Order order) { ... }
}

Notes ​

  • The most common use case is allowing test assemblies to access internal types and members
  • If the declaring assembly is strong-named, the friend assembly must be strong-named too and the attribute must include its full public key (not the public key token)
  • Overusing friend assemblies can weaken encapsulation - prefer it primarily for test projects
  • See Private protected access modifier C# 7.2 for the in-assembly equivalent of exposing internals to one specific assembly.

More information ​

Powered by Intracia