How to: Create Unsigned Friend Assemblies
This example shows how to use friend assemblies with assemblies that are unsigned.
To create an assembly and a friend assembly in Visual Studio
Create a new project for a Windows Forms application named FriendAssembliesB.
On the File menu, point to Add and then click New Project.
In the Add New Project dialog box, click Class Library and name the project FriendAssembliesA.
Replace the content of Class1.vb in the FriendAssembliesA project with the following code. This code uses the InternalsVisibleToAttribute attribute to declare FriendAssembliesB as a friend assembly.
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
Right-click the FriendAssembliesB project in Solution Explorer and then click Add Reference.
In the Add Reference dialog box, click the Projects tab. Click FriendAssembliesA and then click OK.
Right-click Form1.vb in the FriendAssembliesB project, and then click View Code.
Add the following code to the Form1 class.
Because FriendAssembliesA specifies FriendAssembliesB as a friend assembly, the code in FriendAssembliesB can access Friend types and members from FriendAssembliesA.
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) _ Handles MyBase.Load ' Access a Friend class. Dim friendTest1 As New FriendAssembliesA.FriendAssembliesA friendTest1.Test() Dim friendTest2 As New FriendAssembliesA.FriendAssembliesClassA ' Access a Friend method. friendTest2.Test() End Sub
Press F5 to compile and run the project.
The program displays message boxes that contain the strings "Friend Assemblies Sample Class" and "Friend Assemblies Sample Method".
Security
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.
See Also
Tasks
How to: Create Signed Friend Assemblies
Concepts
Friend Assemblies (Visual Basic)