Friend assemblies - .NET (original) (raw)

Skip to main content

This browser is no longer supported.

Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.

Friend assemblies

In this article

A friend assembly is an assembly that can access another assembly's internal (C#) or Friend (Visual Basic) types and members. If you add an assembly attribute to AssemblyA to identify AssemblyB as a friend assembly, you no longer have to mark types and members in AssemblyA as public in order for them to be accessed by AssemblyB. This is especially convenient in the following scenarios:

Remarks

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 AssemblyA 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 in C# or Friend in Visual Basic.

Note

When you compile an assembly like AssemblyB that will access internal types or internal members of another assembly like AssemblyA, 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 OutputAssembly (C#) or -out (Visual Basic).

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");
    }

}
Imports System.Runtime.CompilerServices
<Assembly: InternalsVisibleTo("AssemblyB")>

' Friend class.
Friend Class FriendClass
    Public Sub Test()
        Console.WriteLine("Sample Class")
    End Sub
End Class

' Public class with a Friend method.
Public Class ClassWithFriendMethod
    Friend Sub Test()
        Console.WriteLine("Sample Method")
    End Sub
End Class

Only assemblies that you explicitly specify as friends can access internal (C#) or Friend (Visual Basic) types and members. For example, if AssemblyB is a friend of Assembly A and Assembly C references AssemblyB, Assembly C does not have access to internal (C#) or Friend (Visual Basic) types in Assembly A.

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

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

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

See also

Collaborate with us on GitHub

The source for this content can be found on GitHub, where you can also create and review issues and pull requests. For more information, see our contributor guide.

Additional resources

In this article