
In my most recent article covering VB9 and Visual Studio 2008, I mentioned a new feature of the .NET Framework version 3.5 that allows a class to be decorated with an attribute named "InternalsVisibleTo" that expands the Friend scope outside the current assembly to include another specified assembly. A reader of my article replied to let me know that I hadn't mentioned another feature provided in the .NET Framework version 2.0 and available inside Visual Studio 2005. I didn't know about this feature, so I am passing it along.
The feature I overlooked is an InternalsVisibleTo that can be applied to an assembly, expanding the Friend scope of all classes expand to include another specified assembly. I was excited about the ability to expand a single class's Friend scope, but I am even more excited that I can do it for an entire assembly. This feature is invaluable in good unit testing, and the ability to open an entire assembly with one attribute makes it easier to manage.
Keep in mind a simple rule: if the assembly you want to expose Friend methods from is strongly named (call it CodeAssembly), then the assembly you are exposing these methods to (call it TestAssembly) must also be strongly named. Remembering this will save you a hour of troubleshooting why the Friend methods are not appearing as expected in IntelliSense from TestAssembly.
Inside the CodeAssembly project, add the following line of code - nowhere in particular works great. Once this code is added, all the Friend types from CodeAssembly are visible inside TestAssembly.
<Assembly:InternalsVisibleTo("TestAssembly, PublicKey=PublicKeyHere")>
Getting the public key of an assembly is not as simple as Right-Click, Properties, Version tab - Details tab in Vista. Use the strong name tool, sn.exe as shown below.
C:\Development\TestAssembly\bin>sn -Tp TestAssembly.dll
Microsoft (R) .NET Framework Strong Name Utility Version 2.0.50727.42
Copyright (c) Microsoft Corporation. All rights reserved.
Public key is
00240000048000009400000006020000002400005253413100040000010001006fd1b6f542259a
b2bb4eabb9907f7f997e502b2d64757f4e33bc303c921a57b71d124ea3af1ce2321c8c9f8f372a
5b9c87138b3c09b796037696846ae09e4accac8317f7be1200a5751060794708a0f9184241a502
1c0aa24b279e5bcacc4ec15df4e01fde7761e445005adeb79e8754d9c29b5528dd02c2598cd9f8
99b26cb1
Public key token is 3aa62adff42294b1
Copy the text of the public key into a class inside CodeAssembly as shown below:
'Filename: ProjectLibrary.vb
<Assembly:InternalsVisibleTo("TestAssembly, PublicKey="002400000480000" & _
"09400000006020000002400005253413100040000010001006fd1b6f542259ab2bb4eabb" & _
"9907f7f997e502b2d64757f4e33bc303c921a57b71d124ea3af1ce2321c8c9f8f372a5b9" & _
"c87138b3c09b796037696846ae09e4accac8317f7be1200a5751060794708a0f9184241a" & _
"5021c0aa24b279e5bcacc4ec15df4e01fde7761e445005adeb79e8754d9c29b5528dd02c" & _
"2598cd9f899b26cb1")>
If CodeAssembly is not strongly named, then the syntax of the InternalsVisibleTo attribute is simplified because the public key is not required.
'Filename: ProjectLibrary.vb
<Assembly:InternalsVisibleTo("TestAssembly")>


