InternalsVisibleToAttribute Constructor (String)
Initializes a new instance of the InternalsVisibleToAttribute class with the name of the specified friend assembly.
Assembly: mscorlib (in mscorlib.dll)
Theconstructor 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 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.
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:
Extract the public key from the strong-named key file to a separate file:
Sn -p snk_fileoutfile
Display the full public key to the console:
Sn -tp outfile
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 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.
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.
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:
Available since 8
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Available since 8.1