How to Use InternalsVisibleTo

VS2005 defaults new classes to internal rather than public (although it appears that Orcas defaults to public). This is good as it reduces the public interface of your assembly and it encourages you to actually think about whether a class needs to be public.

You can allow other assemblies access to your internal types and members though, by using the InternalsVisibleTo attribute. This is especially useful for allowing your test assembly to access your internal classes.

This attribute can only be applied to strongly named assemblies.

[assembly:InternalsVisibleTo(“TestAssembly, PublicKey=TestAssembly’s public key“)]

The attribute requires the assembly name and the public key. To retrieve the public key use sn.exe. Open a visual studio command prompt and enter:

sn.exe -Tp AssemblyPath

Alternatively if you use an alternate file explorer you could set up a macro.

One thought on “How to Use InternalsVisibleTo

  1. just a note, at least since .net3.0 (and maybe earlier but not sure where to check) in case anyone is looking at this now, this attribute InternalsVisibleTo can be applied to assemblies without strong names too

    [assembly: InternalsVisibleTo(“my.unsigned.assembly”)]

Leave a Reply