InternalsVisibleTo and PublicKey or PublicKeyToken

A long time since my previous blog as nowadays I often tweet my ramblings but this one does not fit a tweet 🙂

Sometimes you are working with strong named assemblies and when you are having unit tests and want to access internals then you have to use the InternalsVisibleTo assembly attribute. So to discover the public key token I ran “sn.exe –tp project.publickey” and then you get the public key (long) and the public key  token (short).

 

Microsoft (R) .NET Framework Strong Name Utility  Version 4.0.30319.1
Copyright (c) Microsoft Corporation.  All rights reserved.

Public key is
0024000004800000940000000602000000240000525341310004000001000100ff6b089e3c4aaa
a96d775c3e431ea5b8b77fe2364be31793d0baf518ba39477e7fcfc027871d184e169d70ee2940
4dc26dc66c9ab970ad9a3d78be146716e10fdc4b4d3e821a7f402498ccb30f95eaca3922075354
ecdf7bb57b20a20579cba375de71e15f298f779aad7b421f4c0ee5f7c299b528f10574f64e5610
5ea3d7af

Public key token is 67178dccc283ce39

 

So I used the following attribute:

 

[assembly: InternalsVisibleTo("My.Project.Tests, PublicKeyToken=67178dccc283ce39")]

 

And got this nice compiler error:

Friend assembly reference is invalid. Strong-name signed assemblies must specify a public key in their InternalsVisibleTo declarations.

Then I pasted the long variant in the InternalsVisibleTo attribute and it compiled but I knew for 100% that the short version had to work. After investigation there seem two ways to pass the required strong name public key information. You can choose if you want to pass the whole public key or the public key token.

When both assemblies are signed then you need to pass the full PublicKey.

  • Recent Posts
  • Recent Comments
  • Archives
  • Categories
  • Meta