Monday, March 12, 2012

Exposing the Internals of an Assembly to Another Unit Testing Assembly


I have two projects one is production code (called Main), the other is a unit test project to test the real production code (called Main.Test).  Both Main and Main.Test are signed with a strong name key.  To expose the internal stuff to the Main.Test project we have to add an item at the bottom of the AssemblyInfo.cs class (in Main, not Main.Test) which looks similar to this:

(Note:  Names and Public Keys have been changed to protect the innocent, and semi-innocent)

[assembly: System.Runtime.CompilerServices.InternalsVisibleTo("Main.Test, PublicKey=1028000004800030940000100602000000240000525341310004
0000010001001312297cb7c20cfb40f4efef6e5e96dcf84fa46db61b43b2bf65cf
49d547d6a5f14ef07f54d6313b39b10ae631e158748b39e2ab4c816b0753f2a
432160dddf06a1a7170d7ab6e4df081784da7a08b37a3a8a47b722ed6cf6c1c
f6896bc709f805a8087a758156171b7deb74a2ded23ce55688f9b2a67bfeedf1
081496da76f4")]

Now you may be thinking to yourself:  "That's great bob, but how in the hell did you get this value for the public key in the first place?"  Good question!  Here is the answer:

1.  Open a visual studio command prompt and enter this command:

(This is using Visual Studio 2008)
C:\Program Files\Microsoft Visual Studio 9.0\VC>sn -p "[RestOfPath]\Main.Test\MainProjectStrongNameKey.snk" "[RestOfPath]\Main.Test\MainProjectStrongNameKey.publickey"

2.  You should then get a message that says "Public key written blah blah blah".

3.  Now in the same command window, enter this command:

C:\Program Files\Microsoft Visual Studio 9.0\VC>sn -tp "[RestOfPath]\Main.Test\MainProjectStrongNameKey.publickey"

Now the command line should spit out the long public key, which you can then use like I did above.  Now you can test your internal members in the Main project from the Main.Test project
Happy testing!

On the Utility of Visual Basic and Haikus

I was working with some sample code which unfortunately was in Visual Basic.  After working on translating the Visual Basic to a language that doesn't suck (anything else is better) for about 20 minutes I started to try to think of ways to increase the rate I translated the VB code to C#.  My first thought was to write a Haiku about it.  Here it goes:

I hate Visual Basic
The Bubonic Plague
is much more preferable

After writing this I realized that writing Haikus is like writing Visual Basic, both are a waste of time.  ;)

Fast way to Change the Directory Path in a Console Window

I have been aware of this trick for over a year now, but I haven't shared it until now because of my legendary procrastination when it comes to blogging.  If you open a command prompt the usual way this is what you get:

clip_image002

Notice the path defaults to my users folder? So in order to change it I have to enter a command like this: cd C:\ which will change the path to the root C:\ folder. I have learned a better way than doing the cd command to get to arbitrary folders.
If you click on your arbitrary folder, then hold down the Shift key and right click you will see this menu come up:

clip_image004

Notice there is a choice to “Open a Command Window Here?”
Clicking this menu item will give you this:

clip_image006

Woo hoo! No cd command nonsense!

One other thing, you may need to have your command window open with administrator privileges.  here is a link that explains how you can make that happen automatically:

Setting Command Window to Open in Administrator Mode