JM (Jason Meridth)

JM (Jason Meridth)

Learn, Converse, Share

02 Oct 2007

.NET InternalsVisibleTo attribute == C++ Friend Assemblies

To all developers who haven’t heard of this, there is an ability to have a project, say a test project, to have access to another project’s Internal classes. (MSDN definition).

For security reasons, make sure that your test assembly is strongly named.  You can achieve this via the “Signing” tab on the project properties.

Don’t ask why I’m playing with COM projects with .NET (I’m messing with the MSBuild sdc tasks and their automated COM components install tasks)

image

**However,**I still choose to use the Visual Studio command line to create my keys via the Strong Name tool (sn.exe) and then browse for it via the “Signing” tab on project properties. Make sure you are in the same directory as your solution file (.sln)

  • sn -k TestComComponent.snk // Generate strong name key

  • sn -p TestComComponent.snk key.publickey // Extract public key from TestComComponent.snk into key.publickey

  • sn -tp key.publickey // Display public key stored in file ‘key.publickey’

    image

This gives us the public key blob that we need to embed into the InternalsVisibleTo attribute we are going to add to our AssemblyInfo.cs file.

This is what you would need to add to your AssemblyInfo.cs file of your main assembly:

[assembly:InternalsVisibleToAttribute("<TestAssemblyName>, PublicKey=00240000048000...RestOfPublicKeyFromAbove")]

David Kean has even created a tool to create your attribute for you with the public key and everything.  Check it out here.  Blog post here.

Comments

Nelson Montalvo: As an alternative, if you have the strongly named assembly, use a capital T, like so: sn -Tp <> This will extract the public key from the assembly’s manifest.