System.Runtime.CompilerServices.InternalsVisibleToAttribute Class

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

See Also: InternalsVisibleToAttribute Members

Syntax

[System.AttributeUsage(System.AttributeTargets.Assembly, AllowMultiple=true, Inherited=false)]
public sealed class InternalsVisibleToAttribute : Attribute

Remarks

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 System.Runtime.CompilerServices.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.

code reference: System.Runtime.CompilerServices.InternalsVisibleToAttribute#3

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

code reference: System.Runtime.CompilerServices.InternalsVisibleToAttribute#4

The friend assembly is identified by the InternalsVisibleToAttribute.#ctor(string) 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.#ctor(string) constructor must include the full public key as well as the name of the assembly. For more information about using System.Runtime.CompilerServices.InternalsVisibleToAttribute with strong-named assemblies, see the InternalsVisibleToAttribute.#ctor(string) constructor.

Do not include values for the System.Reflection.AssemblyName.CultureInfo, System.Reflection.AssemblyName.Version, or System.Reflection.AssemblyName.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 System.Runtime.CompilerServices.InternalsVisibleToAttribute attribute.

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.

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

Requirements

Namespace: System.Runtime.CompilerServices
Assembly: mscorlib (in mscorlib.dll)
Assembly Versions: 2.0.0.0, 4.0.0.0
Since: .NET 2.0