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

C#
// In AssemblyInfo.cs or a top-level file
[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));
C#
// 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
  • For strong-named assemblies you must include the full public key in the InternalsVisibleTo attribute
  • Overusing friend assemblies can weaken encapsulation - prefer it primarily for test projects

More information