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

  1. Open a Visual Studio command prompt.

  2. 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).

    1. Generate a strong-name key for this example and store it in the file FriendAssemblies.snk:

      sn -k FriendAssemblies.snk

    2. Extract the public key from FriendAssemblies.snk and put it into FriendAssemblies.publickey:

      sn -p FriendAssemblies.snk FriendAssemblies.publickey

    3. Display the public key stored in the file FriendAssemblies.publickey:

      sn -tp FriendAssemblies.publickey

  3. 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
    
  4. Compile and sign FriendAssembliesSnA.vb:

    Vbc.exe /target:library /keyfile:FriendAssemblies.snk FriendAssembliesSnA.vb

  5. 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.

    ' FriendAssembliesSnB.vb. 
    ' Compile with:  
    ' Vbc.exe /keyfile:FriendAssemblies.snk /r:FriendAssembliesSnA.dll FriendAssembliesSnB.vb 
    Module FriendAssembliesSnB
        Public Sub Main()
            ' Access a Friend class. 
            Dim friendTest As New FriendAssembliesSnA
            friendTest.Test()
        End Sub 
    End Module
    
  6. 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).

  7. Run FriendAssembliesSnB.exe.

    The program displays a message box that contains the string "Friend Assemblies Sample".

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 Unsigned Friend Assemblies

Concepts

Friend Assemblies (Visual Basic)

Reference

InternalsVisibleToAttribute

/keyfile

Strong Name Tool (Sn.exe)

Other Resources

Creating and Using Strong-Named Assemblies