|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
How to: Create Signed Friend Assemblies
This example shows how to use friend assemblies with assemblies that have strong names. Both assemblies must be strong named. Although both assemblies in this example use the same keys, you could use different keys for two assemblies.
To create a signed assembly and a friend assembly in Visual Studio
Open a Visual Studio command prompt.
Use the following sequence of commands with the Strong Name tool to generate a keyfile and to display its public key. For more information, see Strong Name Tool (Sn.exe).
Generate a strong-name key for this example and store it in the file FriendAssemblies.snk:
sn -k FriendAssemblies.snk
Extract the public key from FriendAssemblies.snk and put it into FriendAssemblies.publickey:
sn -p FriendAssemblies.snk FriendAssemblies.publickey
Display the public key stored in the file FriendAssemblies.publickey:
sn -tp FriendAssemblies.publickey
Create a new Visual Basic file that is named FriendAssembliesSnA.vb and contains the following code. The code uses the InternalsVisibleToAttribute attribute to declare FriendAssembliesSnB as a friend assembly.
The Strong Name tool generates a new public key every time it runs. Therefore, you must replace the public key in the following code with the public key you just generated, as shown in the following example.
' FriendAssembliesSnA.vb. ' Compile with: ' Vbc.exe /target:library /keyfile:FriendAssemblies.snk FriendAssembliesSnA.vb Imports System.Runtime.CompilerServices ' Replace the value of PublicKey with your public key. <Assembly: InternalsVisibleTo("FriendAssembliesSnB, PublicKey=" & _ "002400000480000094000000060200000024000052534131000400000100010065364c" & _ "640a04ab7c4f06313779601ffb5d1284b381393ad2ede92b4bf91384bbfdb0f9b7ecf6" & _ "a387b7f9348fc59366987429db41ee061c1a808eb408c4ebd9aebd0d4b19b721121a0d" & _ "c66949f12878c4e2add5489f6208f49e67ddee4d0bb72fa6860a775b7e2232209efe8b" & _ "4566ea8bd9319a4b8feb3ef4911e7a7d2e957a92")> ' Friend class. Friend Class FriendAssembliesSnA Public Sub Test() MsgBox("Friend Assemblies Sample") End Sub End Class
Compile and sign FriendAssembliesSnA.vb:
Vbc.exe /target:library /keyfile:FriendAssemblies.snk FriendAssembliesSnA.vb
Create a new Visual Basic file that is named FriendAssembliesSnB.vb and contains the following code. Because FriendAssembliesSnA specified FriendAssembliesSnB as a friend assembly, the code in FriendAssembliesSnB can access Friend types and members from FriendAssembliesSnA.
Compile and sign FriendAssembliesSnB.vb:
Vbc.exe /keyfile:FriendAssemblies.snk /r:FriendAssembliesSnA.dll FriendAssembliesSnB.vb
The name of the assembly generated by the compiler must match the friend assembly name passed to the InternalsVisibleToAttribute attribute. You can explicitly set the assembly by using the /out compiler option. For more information, see /out (Visual Basic).
The program displays a message box that contains the string "Friend Assemblies Sample".
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.