Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
InternalsVisibleToAttribute Class
Collapse the table of content
Expand the table of content

InternalsVisibleToAttribute Class

Specifies that types that are ordinarily visible only within the current assembly are visible to a specified assembly.

System.Object
  System.Attribute
    System.Runtime.CompilerServices.InternalsVisibleToAttribute

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

'Declaration
<AttributeUsageAttribute(AttributeTargets.Assembly, AllowMultiple := True, Inherited := False)> _
Public NotInheritable Class InternalsVisibleToAttribute _
	Inherits Attribute

The InternalsVisibleToAttribute type exposes the following members.

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryInternalsVisibleToAttributeInitializes a new instance of the InternalsVisibleToAttribute class with the name of the specified friend assembly.
Top

  NameDescription
Public propertyAllInternalsVisibleInfrastructure. This property is not implemented.
Public propertySupported by the XNA FrameworkSupported by Portable Class LibraryAssemblyNameGets the name of the friend assembly to which all types and type members that are marked with the internal keyword are to be made visible.
Public propertyTypeIdWhen implemented in a derived class, gets a unique identifier for this Attribute. (Inherited from Attribute.)
Top

  NameDescription
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryEqualsInfrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetHashCodeReturns the hash code for this instance. (Inherited from Attribute.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryGetTypeGets the Type of the current instance. (Inherited from Object.)
Public methodIsDefaultAttributeWhen overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (Inherited from Attribute.)
Public methodSupported by the XNA FrameworkMatchWhen overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Public methodSupported by the XNA FrameworkSupported by Portable Class LibraryToStringReturns a string that represents the current object. (Inherited from Object.)
Top

  NameDescription
Explicit interface implemetationPrivate method_Attribute.GetIDsOfNamesMaps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.GetTypeInfoRetrieves the type information for an object, which can be used to get the type information for an interface. (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.GetTypeInfoCountRetrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute.)
Explicit interface implemetationPrivate method_Attribute.InvokeProvides access to properties and methods exposed by an object. (Inherited from Attribute.)
Top

Ordinarily, types and members with internal scope (in C#) and friend scope (in Visual Basic) are visible only in the assembly in which they are defined. The InternalsVisibleToAttribute attribute makes them also visible to the types in a specified assembly, which is known as a friend assembly.

The attribute is applied at the assembly level. This means that it can be included at the beginning of a source code file, or it can be included in the AssemblyInfo file in a Visual Studio project. You can use the attribute to specify a single friend assembly that can access the internal types and members of the current assembly. You can define multiple friend assemblies in two ways. They can appear as individual assembly-level attributes, as the following example illustrates.

<assembly:InternalsVisibleTo("Friend1a")>
<assembly:InternalsVisibleTo("Friend1b")>

They can also appear with separate InternalsVisibleToAttribute tags but a single assembly keyword, as the following example illustrates.

<Assembly:InternalsVisibleTo("Friend2a"), _
 Assembly:InternalsVisibleTo("Friend2b")>

The friend assembly is identified by the InternalsVisibleToAttribute constructor. Both the current assembly and the friend assembly must be unsigned, or both assemblies must be signed with a strong name. If they are signed with a strong name, the argument to the InternalsVisibleToAttribute constructor must include the full public key as well as the name of the assembly. For more information about using InternalsVisibleToAttribute with strong-named assemblies, see the InternalsVisibleToAttribute constructor.

Do not include values for the CultureInfo, Version, or ProcessorArchitecture field in the argument; the Visual Basic, C#, and C++ compilers treat this as a compiler error. If you use a compiler that does not treat it as an error (such as the MSIL Assembler (ILAsm.exe)) and the assemblies are strong-named, a MethodAccessException exception is thrown the first time the specified friend assembly accesses the assembly that contains the InternalsVisibleToAttribute attribute.

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

Compiling the friend assembly with C#

If you use the C# compiler to compile the friend assembly, you must explicitly specify the name of the output file (.exe or .dll) by using the /out compiler option. This is required because the compiler has not yet generated the name for the assembly it is building at the time it is binding to external references. The /out compiler option is optional for the Visual Basic compiler, and the corresponding -out or -o compiler option should not be used when compiling friend assemblies with the F# compiler.

Compiling the friend assembly with C++

In C++, in order to make the internal members enabled by the InternalsVisibleToAttribute attribute accessible to a friend assembly, you must use the as_friend attribute in the C++ #using directive directive. For more information, see Friend Assemblies (C++).

Signed assemblies

The following example uses the InternalsVisibleToAttribute attribute to make an internal method named AppendDirectorySeparator in a signed assembly visible to another signed assembly. It 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, the Example.Main method in Friend1 can successfully call the FileUtilities.AppendDirectorySeparator method, although the method is internal to the Assembly1 assembly. Note that if you are compiling in C# from the command line, you must use the /out compiler switch to ensure that the name of the friend assembly is available when the compiler binds to external references.


' 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\

Unsigned assemblies

The following example uses the InternalsVisibleToAttribute attribute to make an internal member of an unsigned assembly visible to another unsigned assembly. The attribute ensures that the internal StringLib.IsFirstLetterUpperCase method in an assembly named UtilityLib is visible to the code in an assembly named Friend2. The following is the source code for UtilityLib.dll:


Imports System.Runtime.CompilerServices

<assembly: InternalsVisibleTo("Friend2")>

Namespace Utilities.StringUtilities
   Public Class StringLib
      Friend Shared Function IsFirstLetterUpperCase(s As String) As Boolean 
         Dim first As String = s.Substring(0, 1)
         Return first = first.ToUpper()
      End Function 
   End Class 
End Namespace

The following example provides the source code for the Friend2 assembly. Note that if you are compiling in C# from the command line, you must use the /out compiler switch to ensure that the name of the friend assembly is available when the compiler binds to external references.

Imports Utilities.StringUtilities

Module Example
   Public Sub Main()
      Dim s As String = "The Sign of the Four"
      Console.WriteLine(StringLib.IsFirstLetterUpperCase(s))
   End Sub 
End Module

.NET Framework

Supported in: 4.6, 4.5, 4, 3.5, 3.0, 2.0

.NET Framework Client Profile

Supported in: 4, 3.5 SP1

XNA Framework

Supported in: 3.0, 2.0, 1.0

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Show:
© 2015 Microsoft