Here is a little bit of knowledge that lots of people are not aware of. There is an Attribute that is InternalsVisibleToAttribute that allows access to a specific external project (the unit test project).
This attribute, once inserted inside AssemblyInfo.cs, will grant "public" visibility to all internal members of the project to the project specified within the attribute.
Here is how it is shown on MSDN:
[assembly:InternalsVisibleTo("MyAssembly, PublicKey=32ab4ba45e0a69a1")]
It is however wrong and will never work. The main reason is that what is there let us believe that it's the PublicKeyToken but it is in fact the PublicKey as clearly typed there.
So... how do we get this PublicKey? By executing the following command:
sn -Tp MyAssembly.dll
The result is going to be something like the following:
Public key is 0024000004800000940000000602000000240000525341310004000001000100adfedd2329a0f8 3e057f0b14e47f02ec865e542c2dcca6349177fe3530edd5080276c48c6d02fa0a6f67738cc1a0 793be3322cf17b8995acc15055c00fa61b67a203c7eb2516922810ff0b17cd2e08492bdcafc4a9 23e6fff4caba672a4c2d0d0f5cac9aea95c3dce3717bb733d852c387f5f025c42c14ec8d759f7e b13689bePublic key token is 96dfc321948ee54c
Here is the end result to make it properly visible:
[assembly: InternalsVisibleTo("AssemblyB, PublicKey=" + "0024000004800000940000000602000000240000525341310004000001000100adfedd2329a0f8" + "3e057f0b14e47f02ec865e542c2dcca6349177fe3530edd5080276c48c6d02fa0a6f67738cc1a0" + "793be3322cf17b8995acc15055c00fa61b67a203c7eb2516922810ff0b17cd2e08492bdcafc4a9" + "23e6fff4caba672a4c2d0d0f5cac9aea95c3dce3717bb733d852c387f5f025c42c14ec8d759f7e" + "b13689be" )]
I created a GUI tool that generates InernalVisibleTo code for you. So there's no need to use sn tool from the command prompt. A developer just have to paste the code into AssemblyInfor file.
ReplyDeleteYou can download it from www.kreynin.net/InternalsToVisible/AssemblyHelper.zip .
Or you can read more about at tinyurl.com/cw8guw