Rory Primrose

Learn from my mistakes, you don't have time to make them yourself

View project on GitHub

GetPublicKey - InternalsVisibleTo

I posted previously about using the InternalsVisibleTo attribute for unit testing and how I had come across David Kean’s very helpful PublicKey application. I have been using this application for the last month or so and it has been great, until yesterday.

I changed the snk file used by my solution. This caused an interesting Catch-22 situation. AssemblyA couldn’t compile because it had an InternalsVisibleTo attribute pointing to AssemblyATest, which now has the wrong PublicKey value. AssemblyATest couldn’t compile because it directly references AssemblyA in order to run the tests.

Unfortunately, David’s PublicKey application works from binaries alone. Because I can’t compile the assemblies, I can’t regenerate the InternalsVisibleTo attribute with the correct PublicKey value.

Now there are several ways around this, but I couldn’t resist coding my own utility to cover this scenario. Using David’s application as inspiration, I have created GetPublicKey. It will identity the PublicKey value from dll, exe, snk and pub files in order to generate an InternalsVisibleTo attribute.

GetPublicKey looks at command line arguments so that you can send any of the supported file types to it using Explorer, VS External Tools or VS Open With. It leverages sn.exe to extract the public key information so you may need to install the SDK.

I have also found GetPublicKey very helpful to get the PublicKeyToken values (found in the log data in the UI). I often use this for configuration values that define assembly qualified names of types that are in strong named assemblies.

Download: GetPublicKey.exe (127.50 kb)

Written on December 6, 2007