|
46 | 46 | <remarks> |
47 | 47 | <format type="text/markdown">< or [`Friend` scope in Visual Basic](/dotnet/visual-basic/language-reference/modifiers/friend) are visible only in the assembly in which they are defined. Types and members with [`protected internal`](/dotnet/csharp/language-reference/keywords/protected-internal) scope ([`Protected Friend`](/dotnet/visual-basic/language-reference/modifiers/protected-friend) scope in Visual Basic) are visible only in their own assembly or to types that derive from their containing class. Types and members with [`private protected`](/dotnet/csharp/language-reference/keywords/private-protected) scope ([`Private Protected`](/dotnet/visual-basic/language-reference/modifiers/private-protected) scope in Visual Basic) are visible in the containing class or in types that derive from their containing class within the current assembly |
| 52 | + |
| 53 | + The <xref:System.Runtime.CompilerServices.InternalsVisibleToAttribute> attribute makes these types and members also visible to the types in a specified assembly, which is known as a friend assembly. This applies only to `internal` (`Friend` in Visual Basic), `protected internal`(`Protected Friend` in Visual Basic), and `private protected` (`Private Protected` in Visual Basic) members, but not `private` ones. |
| 54 | + |
| 55 | + > [!NOTE] |
| 56 | + > In the case of `private protected` (`Private Protected` in Visual Basic) members, the <xref:System.Runtime.CompilerServices.InternalsVisibleToAttribute> attribute extends accessibility only to types that derive from the *containing class* of the member. |
51 | 57 | |
52 | 58 | 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. |
53 | 59 | |
|
67 | 73 | |
68 | 74 | Do not include values for the <xref:System.Reflection.AssemblyName.CultureInfo%2A>, <xref:System.Reflection.AssemblyName.Version%2A>, or <xref:System.Reflection.AssemblyName.ProcessorArchitecture%2A> 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)](/dotnet/framework/tools/ilasm-exe-il-assembler)) and the assemblies are strong-named, a <xref:System.MethodAccessException> exception is thrown the first time the specified friend assembly accesses the assembly that contains the <xref:System.Runtime.CompilerServices.InternalsVisibleToAttribute> attribute. |
69 | 75 | |
70 | | - For more information about how to use this attribute, see the following topics: |
71 | | - |
72 | | -- [Friend Assemblies (C++)](/cpp/dotnet/friend-assemblies-cpp) |
73 | | - |
74 | | -- [Friend assemblies](/dotnet/standard/assembly/friend) |
| 76 | + For more information about how to use this attribute, see [Friend assemblies](/dotnet/standard/assembly/friend) and [C++ friend assemblies](/cpp/dotnet/friend-assemblies-cpp). |
75 | 77 | |
76 | 78 | <a name="Key"></a> |
77 | | -## Getting the full public key |
| 79 | +## Get the full public key |
78 | 80 | You can use the [Strong Name Tool (Sn.exe)](/dotnet/framework/tools/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: |
79 | 81 | |
80 | 82 | 1. Extract the public key from the strong-named key file to a separate file: |
|
87 | 89 | |
88 | 90 | 3. Copy and paste the full public key value into your source code. |
89 | 91 | |
90 | | -## Compiling the friend assembly with C# |
| 92 | +## Compile the friend assembly with C# |
91 | 93 | 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. |
92 | 94 | |
93 | | -## Compiling the friend assembly with C++ |
94 | | - In C++, in order to make the internal members enabled by the <xref:System.Runtime.CompilerServices.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++)](/cpp/dotnet/friend-assemblies-cpp). |
95 | | - |
96 | | - |
| 95 | +## Compile the friend assembly with C++ |
| 96 | + In C++, in order to make the internal members enabled by the <xref:System.Runtime.CompilerServices.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++)](/cpp/dotnet/friend-assemblies-cpp). |
97 | 97 | |
98 | 98 | ## Examples |
99 | 99 | **Signed assemblies** |
|
171 | 171 | 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](/dotnet/standard/assembly/create-use-strong-named).) 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. |
172 | 172 | |
173 | 173 | > [!IMPORTANT] |
174 | | -> 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. |
| 174 | +> 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. |
175 | 175 | |
176 | 176 | You can use [Sn.exe (Strong Name Tool)](/dotnet/framework/tools/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: |
177 | 177 | |
|
185 | 185 | |
186 | 186 | 3. Copy and paste the full public key value into your source code. |
187 | 187 | |
188 | | - For more information about how to use the <xref:System.Runtime.CompilerServices.InternalsVisibleToAttribute> attribute, see the following topics: |
| 188 | + For more information about how to use the <xref:System.Runtime.CompilerServices.InternalsVisibleToAttribute> attribute, see the following articles: |
189 | 189 | |
190 | 190 | - [Friend Assemblies (C++)](/cpp/dotnet/friend-assemblies-cpp) |
191 | 191 | |
192 | | -- [Friend assemblies](/dotnet/standard/assembly/friend) |
193 | | - |
194 | | - |
| 192 | +- [Friend assemblies](/dotnet/standard/assembly/friend) |
195 | 193 | |
196 | 194 | ## Examples |
197 | 195 | **Signed assemblies** |
|
0 commit comments