Using InternalsVisibleTo Attribute with Strong Named Assemblies
The InternalsVisibleTo Attribute can be used to allow another assembly (such as your Unit Test assembly) to see the internal members of your assembly. I prefer this method to either putting unit tests in my primary assembly or to using the VSTS feature of autogenerating accessors to my private members. (Those are real fun when you go to refactor a project…)
Now most of the info you will find about this refers to the earlier beta usage. The attribute used to take in a PublicKeyToken but now takes in a PublicKey.
To use the InternalsVisibleTo Attribute:
1. Run sn.exe -p MyStrongNameKey.snk MyStrongNameKey.PublicKey
This will extract to public key to a file with the .PublicKey extension. (I hate using the .pub extension because it is seen as a Microsoft Publisher file…)
2. run sn.exe -tp MyStrongNameKey.PublicKey
This will display your public key for you. Copy this key.
3. Create an attribute as such:
[assembly: InternalsVisibleTo( "MyProject.UnitTests, PublicKey=PASTE-YOUR-PUBLIC-KEY-HERE" )]
4. Note that the public key that was displayed was 5 lines or so. Remove the line breaks from the public key and paste it into your attribute.
5. You are done! The assembly MyProject.UnitTests can now see the internal members of your assembly.