Friend Assemblies (C#)

A friend assembly is an assembly that can access another assembly's internal types and members. If you identify an assembly as a friend assembly, you no longer have to mark types and members as public in order for them to be accessed by other assemblies. This is especially convenient in the following scenarios:

  • During unit testing, when test code runs in a separate assembly but requires access to members in the assembly being tested that are marked as internal .

  • When you are developing a class library and additions to the library are contained in separate assemblies but require access to members in existing assemblies that are marked as internal .


You can use the InternalsVisibleToAttribute attribute to identify one or more friend assemblies for a given assembly. The following example uses the InternalsVisibleToAttribute attribute in assembly A and specifies assembly AssemblyB as a friend assembly. This gives assembly AssemblyB access to all types and members in assembly A that are marked as internal.


When you compile an assembly (assembly AssemblyB) that will access internal types or internal members of another assembly (assembly A), you must explicitly specify the name of the output file (.exe or .dll) by using the /out compiler option. This is required because the compiler has not yet generated the name for the assembly it is building at the time it is binding to external references. For more information, see /out (C#) .

using System.Runtime.CompilerServices;  
using System;  
[assembly: InternalsVisibleTo("AssemblyB")]  
// The class is internal by default.  
class FriendClass  
    public void Test()  
        Console.WriteLine("Sample Class");  
// Public class that has an internal method.  
public class ClassWithFriendMethod  
    internal void Test()  
        Console.WriteLine("Sample Method");  

Only assemblies that you explicitly specify as friends can access internal types and members. For example, if assembly B is a friend of assembly A and assembly C references assembly B, C does not have access to internal types in A.

The compiler performs some basic validation of the friend assembly name passed to the InternalsVisibleToAttribute attribute. If assembly A declares B as a friend assembly, the validation rules are as follows:

  • If assembly A is strong named, assembly B must also be strong named. The friend assembly name that is passed to the attribute must consist of the assembly name and the public key of the strong-name key that is used to sign assembly B.

    The friend assembly name that is passed to the InternalsVisibleToAttribute attribute cannot be the strong name of assembly B: do not include the assembly version, culture, architecture, or public key token.

  • If assembly A is not strong named, the friend assembly name should consist of only the assembly name. For more information, see How to: Create Unsigned Friend Assemblies (C#).

  • If assembly B is strong named, you must specify the strong-name key for assembly B by using the project setting or the command-line /keyfile compiler option. For more information, see How to: Create Signed Friend Assemblies (C#).

The StrongNameIdentityPermission class also provides the ability to share types, with the following differences:

  • StrongNameIdentityPermission applies to an individual type, while a friend assembly applies to the whole assembly.

  • If there are hundreds of types in assembly A that you want to share with assembly B, you have to add StrongNameIdentityPermission to all of them. If you use a friend assembly, you only need to declare the friend relationship once.

  • If you use StrongNameIdentityPermission, the types you want to share have to be declared as public. If you use a friend assembly, the shared types are declared as internal.

For information about how to access an assembly's internal types and methods from a module file (a file with the .netmodule extension), see /moduleassemblyname (C#).

See also