Click to Rate and Give Feedback

  Switch on low bandwidth view
This page is specific to
Microsoft Visual Studio 2008/.NET Framework 3.5

Other versions are also available for the following:
.NET Framework Class Library
InternalsVisibleToAttribute Class

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)
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

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.

The following example makes types and type members that are marked with the internal keyword in the declaring assembly visible to AssemblyB, to all versions of AssemblyB, and to all variants of AssemblyB that might contain different cultures.

[assembly:InternalsVisibleTo("AssemblyB, PublicKey=32ab4ba45e0a69a1")]
System..::.Object
  System..::.Attribute
    System.Runtime.CompilerServices..::.InternalsVisibleToAttribute
Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

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.

.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
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Generates a warning in VS2008      Burt Harris   |   Edit   |   Show History

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 What's this?: Add a tag
Flag as ContentBug
You need to sign your unit test assembly if the assembly under test is strongly named.      An Phu   |   Edit   |   Show History

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 What's this?: Add a tag
Flag as ContentBug
Multiple Assemblies      HWM ... Thomas Lee   |   Edit   |   Show History

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.

Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker