Friend Assemblies (Visual Basic)
[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]
A friend assembly is an assembly that is permitted to access another assembly's Friend types and members. (For more information, see Friend (Visual Basic).) 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 Friend.
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 Friend.
You can use the InternalsVisibleToAttribute attribute to identify one or more friend assemblies for a given assembly. For example, if you include the InternalsVisibleToAttribute attribute in assembly A and specify assembly B as a friend assembly, assembly B will have access to all types and members in assembly A that are marked as Friend, as shown in the following example.
Imports System.Runtime.CompilerServices <Assembly: InternalsVisibleTo("FriendAssembliesB")> ' Friend class. Friend Class FriendAssembliesA Public Sub Test() MsgBox("Friend Assemblies Sample Class") End Sub End Class ' Public class with a Friend method. Public Class FriendAssembliesClassA Friend Sub Test() MsgBox("Friend Assemblies Sample Method") End Sub End Class
Only assemblies that you explicitly specify as friends can access Friend 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 Friend 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.
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.
For information about how to access an assembly's Friend types and methods from a module file (a file with the .netmodule extension), see /moduleassemblyname.
There are similarities between the InternalsVisibleToAttribute attribute and the StrongNameIdentityPermission class. The main difference is that StrongNameIdentityPermission can demand security permissions to run a particular section of code, whereas the InternalsVisibleToAttribute attribute controls the visibility of Friend types and members.