InternalsVisibleToAttribute(String) Constructor

Definition

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

C#
public InternalsVisibleToAttribute(string assemblyName);

Parameters

assemblyName
String

The name of a friend assembly.

Examples

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.

C#
//
// The source code should be saved in a file named Example1.cs. It 
// can be compiled at the command line as follows:
//
//    csc /t:library /keyfile:<snkfilename> Assembly1.cs
//
// The public key of the Friend1 file should be changed to the full
// public key stored in your strong-named key file.
//
using System;
using System.IO;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Friend1, PublicKey=002400000480000094" + 
                              "0000000602000000240000525341310004000" +
                              "001000100bf8c25fcd44838d87e245ab35bf7" +
                              "3ba2615707feea295709559b3de903fb95a93" +
                              "3d2729967c3184a97d7b84c7547cd87e435b5" +
                              "6bdf8621bcb62b59c00c88bd83aa62c4fcdd4" +
                              "712da72eec2533dc00f8529c3a0bbb4103282" +
                              "f0d894d5f34e9f0103c473dce9f4b457a5dee" +
                              "fd8f920d8681ed6dfcb0a81e96bd9b176525a" +
                              "26e0b3")]

public class FileUtilities
{
   internal static string AppendDirectorySeparator(string dir)
   {
      if (!dir.Trim().EndsWith(Path.DirectorySeparatorChar.ToString()))
         return dir.Trim() + Path.DirectorySeparatorChar;
      else
         return dir;
   }
}

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

C#
//
// The source code should be saved in a file named Friend1.cs. It 
// can be compiled at the command line as follows:
//
//    csc /r:Assembly1.dll /keyfile:<snkfilename> /out:Friend1.dll Friend1.cs
//
// 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.
//
using System;

public class Example
{
   public static void Main()
   {
      string dir = @"C:\Program Files";
      dir = FileUtilities.AppendDirectorySeparator(dir);
      Console.WriteLine(dir);
   }
}
// The example displays the following output:
//       C:\Program Files\

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:

C#
using System;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleToAttribute("Friend2")]

namespace Utilities.StringUtilities
{
   public class StringLib
   {
      internal static bool IsFirstLetterUpperCase(String s)
      {
         string first = s.Substring(0, 1);
         return first == first.ToUpper();
      }
   }
}

Unsigned assemblies

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.

C#
using System;
using Utilities.StringUtilities;

public class Example
{
   public static void Main()
   {
      String s = "The Sign of the Four";
      Console.WriteLine(StringLib.IsFirstLetterUpperCase(s));
   }
}

Remarks

The InternalsVisibleToAttribute constructor defines a friend assembly, which is an assembly that has access to the internal and private protected 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 Create and use strong-named assemblies.) If both are unsigned, the assemblyName argument 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 without its directory path or file name extension, 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 argument.

Important

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.

You can use Sn.exe (Strong Name Tool) 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 articles:

Applies to

Product Versions
.NET Core 1.0, Core 1.1, Core 2.0, Core 2.1, Core 2.2, Core 3.0, Core 3.1, 5, 6, 7, 8, 9, 10
.NET Framework 2.0, 3.0, 3.5, 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 1.0, 1.1, 1.2, 1.3, 1.4, 1.5, 1.6, 2.0, 2.1
UWP 10.0