System.Runtime.CompilerServ ...


.NET Framework Class Library
InternalsVisibleToAttribute Class

Updated: July 2009

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

Namespace:  System.Runtime.CompilerServices
Assembly:  mscorlib (in mscorlib.dll)
Syntax

Visual Basic (Declaration)
<AttributeUsageAttribute(AttributeTargets.Assembly, AllowMultiple := True, Inherited := False)> _
Public NotInheritable Class InternalsVisibleToAttribute _
    Inherits Attribute
Visual Basic (Usage)
Dim instance As InternalsVisibleToAttribute
C#
[AttributeUsageAttribute(AttributeTargets.Assembly, AllowMultiple = true, Inherited = false)]
public sealed class InternalsVisibleToAttribute : Attribute
Visual C++
[AttributeUsageAttribute(AttributeTargets::Assembly, AllowMultiple = true, Inherited = false)]
public ref class InternalsVisibleToAttribute sealed : public Attribute
JScript
public final class InternalsVisibleToAttribute extends Attribute
Remarks

This attribute applies to internal types in C#, friend types in Visual Basic, and all types at namespace or global scope in C++. To apply this attribute to a strong-named friend assembly, you must know the hexadecimal string that represents the public key of the friend assembly. For more information, see How to: Sign an Assembly with a Strong Name.

Do not include values for the CultureInfo, Version, or ProcessorArchitecture fields in the string parameter of the friend assembly declaration. The Visual Basic, C#, and C++ compilers treat this as a compile error. If you use a compiler (such as the MSIL Assembler (Ilasm.exe)) that does not treat it as an error and the assemblies are strong-named, a MethodAccessException is thrown the first time the specified friend assembly accesses the assembly that contains InternalsVisibleToAttribute.

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

Inheritance Hierarchy

System..::.Object
  System..::.Attribute
    System.Runtime.CompilerServices..::.InternalsVisibleToAttribute
Thread Safety

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.
Platforms

Windows 7, Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2008 R2, Windows Server 2008, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98, Windows CE, Windows Mobile for Smartphone, Windows Mobile for Pocket PC, Xbox 360, Zune

The .NET Framework and .NET Compact Framework do not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.
Version Information

.NET Framework

Supported in: 3.5, 3.0, 2.0

.NET Compact Framework

Supported in: 3.5, 2.0

XNA Framework

Supported in: 3.0, 2.0, 1.0
See Also

Reference

Change History

Date

History

Reason

July 2009

Added links; removed incomplete example.

Information enhancement.

Tags :


Community Content

Burt Harris
Generates a warning in VS2008

Using the C# example sited above:

[assembly:InternalsVisibleTo("AssemblyB, PublicKey=32ab4ba45e0a69a1")]
produces the following warning:

warning CS1700: Assembly reference 'AssemblyB, PublicKey=32ab4ba45e0a69a1' is invalid and cannot be resolved

the problem appears to be that hex number specified in the example is a Public Key Token, and that the Attribute requires the much longer Publick Key. This can be obtained using strong name utility using the following case-sensitive command:

C:> sn -Tp AssemblyB.dll
Microsoft (R) .NET Framework Strong Name Utility  Version 3.5.21022.8
Copyright (c) Microsoft Corporation. All rights reserved.
Public key is
0024000004800000940000000602000000240000525341310004000001000100adfedd2329a0f8
3e057f0b14e47f02ec865e542c2dcca6349177fe3530edd5080276c48c6d02fa0a6f67738cc1a0
793be3322cf17b8995acc15055c00fa61b67a203c7eb2516922810ff0b17cd2e08492bdcafc4a9
23e6fff4caba672a4c2d0d0f5cac9aea95c3dce3717bb733d852c387f5f025c42c14ec8d759f7e
b13689be
Public key token is 96dfc321948ee54c

Then a correctly-formed attribute would look like this:

[assembly: InternalsVisibleTo("AssemblyB, PublicKey=" 
+ "0024000004800000940000000602000000240000525341310004000001000100adfedd2329a0f8"
+ "3e057f0b14e47f02ec865e542c2dcca6349177fe3530edd5080276c48c6d02fa0a6f67738cc1a0"
+ "793be3322cf17b8995acc15055c00fa61b67a203c7eb2516922810ff0b17cd2e08492bdcafc4a9"
+ "23e6fff4caba672a4c2d0d0f5cac9aea95c3dce3717bb733d852c387f5f025c42c14ec8d759f7e"
+ "b13689be" )]

Tags :

An Phu
You need to sign your unit test assembly if the assembly under test is strongly named.

If

  1. the main assembly (AssemblyA) is strongly named
  2. and you want to test/use its internal types/methods,

then,
the unit test assembly (AssemblyB) needs to be strongly named.

In other words, if you sign your assembly, be sure to sign your unit test assembly as well.

Notes:
If the main assembly is not strongly named
OR
if the main assembly is strongly named but you are not using/testing its internal types/methods, you can get away with just specifying the following without a public key.

[assembly: InternalsVisibleTo("AssemblyB")]
Tags :

Geert VL
Multiple Assemblies

It is not documented anywhere to my knowledge, but if you want to grant "InternalsVisibleTo" permission to more than one assembly, you need to understand the syntax.

To do this you should NOT insert multiples instances of:

  

[assembly: InternalsVisibleTo("FirstAssembly")]

Instead do this:

  

[assembly: InternalsVisibleTo("FirstAssembly"), InternalsVisibleTo("SecondAssembly"),

InternalsVisibleTo("ThirdAssembly")

The former syntax is legal but fails, because each instance simply redefines and replaces any earlier ones, the latter syntax works as required.


The above is not correct.
You can insert multiple instances of InternalsVisibleTo. (Both notations are fine).


Page view tracker