Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
InternalsVisibleToAttribute Constructor

InternalsVisibleToAttribute Constructor

Updated: December 2009

Initializes a new instance of the InternalsVisibleToAttribute class with the name of the specified friend assembly.

Namespace:  System.Runtime.CompilerServices
Assembly:  mscorlib (in mscorlib.dll)

'Declaration
Public Sub New ( _
	assemblyName As String _
)
'Usage
Dim assemblyName As String 

Dim instance As New InternalsVisibleToAttribute(assemblyName)

Parameters

assemblyName
Type: System.String

The name of a friend assembly.

The InternalsVisibleToAttribute constructor defines a friend assembly, which is an assembly that has access to the internal types and members of the current assembly.

Both the current assembly and the friend assembly must be unsigned, or both must be signed with a strong name. (For more information about strong-named assemblies, see Creating and Using Strong-Named Assemblies.) If both are unsigned, the assemblyName parameter consists of the name of the friend assembly, specified without a directory path or file extension. If both are signed, assemblyName consists of the name of the friend assembly along with its full public key (but not its public key token). The other components of a strong name, such as those that provide culture, version, or processor architecture information, cannot be specified in the assemblyName parameter.

You can use Strong Name Tool (Sn.exe) to retrieve the full public key from a strong-named key (.snk) file. To do this, you perform the following steps:

  1. Extract the public key from the strong-named key file to a separate file:

    Sn -p snk_file outfile

  2. Display the full public key to the console:

    Sn -tp outfile

  3. Copy and paste the full public key value into your source code.

For more information about how to use the InternalsVisibleToAttribute attribute, see the following topics:

The following example defines a FileUtilities class that includes an internal AppendDirectorySeparator method. The InternalsVisibleToAttribute attribute is applied to the assembly that contains the FileUtilities class. The attribute allows an assembly named Friend1 to access this internal member.


' The source code should be saved in a file named Example1.cs. It  
' can be compiled at the command line as follows: 

'    vbc Assembly1.vb /t:library /keyfile:<snkfilename>  

' The public key of the Friend1 file should be changed to the full 
' public key stored in your strong-named key file. 

Imports System.IO
Imports System.Runtime.CompilerServices

<Assembly:InternalsVisibleTo("Friend1, PublicKey=002400000480000094" + _
                             "0000000602000000240000525341310004000" + _
                             "001000100bf8c25fcd44838d87e245ab35bf7" + _
                             "3ba2615707feea295709559b3de903fb95a93" + _
                             "3d2729967c3184a97d7b84c7547cd87e435b5" + _
                             "6bdf8621bcb62b59c00c88bd83aa62c4fcdd4" + _
                             "712da72eec2533dc00f8529c3a0bbb4103282" + _
                             "f0d894d5f34e9f0103c473dce9f4b457a5dee" + _
                             "fd8f920d8681ed6dfcb0a81e96bd9b176525a" + _
                             "26e0b3")>

Public Class FileUtilities
   Friend Shared Function AppendDirectorySeparator(dir As String) As String 
      If Not dir.Trim().EndsWith(Path.DirectorySeparatorChar) Then 
         Return dir.Trim() + Path.DirectorySeparatorChar
      Else 
         Return dir
      End If    
   End Function 
End Class

If the following example is compiled into a strong-named assembly named Friend1, it can successfully call the FileUtilities.AppendDirectorySeparator method, even though the method is internal to the Assembly1 assembly.


' The source code should be saved in a file named Friend1.vb. It  
' can be compiled at the command line as follows: 

'    vbc Friend1.vb /r:Assembly1.dll /keyfile:<snkfilename>  

' The public key of the Friend1 assembly should correspond to the public key 
' specified in the class constructor of the InternalsVisibleTo attribute in the 
' Assembly1 assembly. 

Module Example
   Public Sub Main()
      Dim dir As String = "C:\Program Files"
      dir = FileUtilities.AppendDirectorySeparator(dir)
      Console.WriteLine(dir)
   End Sub 
End Module 
' The example displays the following output: 
'       C:\Program Files\

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0

Date

History

Reason

December 2009

Expanded the Remarks section and added an example.

Customer feedback.

July 2009

Added links; removed incomplete example.

Information enhancement.

Community Additions

ADD
Show:
© 2015 Microsoft