InternalsVisibleToAttribute Class
Specifies that types that are ordinarily visible only within the current assembly are visible to a specified assembly.
Assembly: mscorlib (in mscorlib.dll)
| Name | Description | |
|---|---|---|
![]() | InternalsVisibleToAttribute(String^) | Initializes a new instance of the InternalsVisibleToAttribute class with the name of the specified friend assembly. |
| Name | Description | |
|---|---|---|
![]() | AllInternalsVisible | This API supports the product infrastructure and is not intended to be used directly from your code. This property is not implemented. |
![]() | AssemblyName | Gets 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. |
![]() | TypeId |
| Name | Description | |
|---|---|---|
![]() | Equals(Object^) | This API supports the product infrastructure and is not intended to be used directly from your code. Returns a value that indicates whether this instance is equal to a specified object.(Inherited from Attribute.) |
![]() | GetHashCode() | Returns the hash code for this instance.(Inherited from Attribute.) |
![]() | GetType() | |
![]() | IsDefaultAttribute() | When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class.(Inherited from Attribute.) |
![]() | Match(Object^) | When overridden in a derived class, returns a value that indicates whether this instance equals a specified object.(Inherited from Attribute.) |
![]() | ToString() | Returns a string that represents the current object.(Inherited from Object.) |
| Name | Description | |
|---|---|---|
![]() ![]() | _Attribute::GetIDsOfNames(Guid%, IntPtr, UInt32, UInt32, IntPtr) | Maps a set of names to a corresponding set of dispatch identifiers.(Inherited from Attribute.) |
![]() ![]() | _Attribute::GetTypeInfo(UInt32, UInt32, IntPtr) | Retrieves the type information for an object, which can be used to get the type information for an interface.(Inherited from Attribute.) |
![]() ![]() | _Attribute::GetTypeInfoCount(UInt32%) | Retrieves the number of type information interfaces that an object provides (either 0 or 1).(Inherited from Attribute.) |
![]() ![]() | _Attribute::Invoke(UInt32, Guid%, UInt32, Int16, IntPtr, IntPtr, IntPtr, IntPtr) | Provides access to properties and methods exposed by an object.(Inherited from Attribute.) |
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.
They can also appear with separateInternalsVisibleToAttribute tags but a single assembly keyword, as the following example illustrates.
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 both assemblies are unsigned, the assemblyName argument consists of the name of the friend assembly, specified without a directory path or file name extension.
If both assemblies are signed with a strong name, the argument to the InternalsVisibleToAttribute constructor must consist of the name of the assembly without its directory path or file name extension, along with the full public key (and not its public key token). To get the full public key of a strong-named assembly, see the Getting the full public key section later in this article. 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 IL 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:
You can use the 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:
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.
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.
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++ 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.
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 assembly that exposes its internal types to this assembly should be // named Assembly1.dll. // // The public key of this assembly should correspond to the public key // specified in the class constructor of the InternalsVisibleTo attribute in the // Assembly1 assembly. // #using <Assembly1.dll> as_friend using namespace System; void main() { String^ dir = L"C:\\Program Files"; dir = FileUtilities::AppendDirectorySeparator(dir); Console::WriteLine(dir); } // 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:
Available since 8
.NET Framework
Available since 2.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 2.0
Windows Phone Silverlight
Available since 7.0
Windows Phone
Available since 8.1
Any public static ( Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
.jpeg?cs-save-lang=1&cs-lang=cpp)
.jpeg?cs-save-lang=1&cs-lang=cpp)
.jpeg?cs-save-lang=1&cs-lang=cpp)
.jpeg?cs-save-lang=1&cs-lang=cpp)
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.