Geeks With Blogs
Jonathan Starr's Blog Thoughts on C#, Ajax, WCF, LINQ, Agile et al.

improve my => 'code'

.. though it is not very well documented.

I have found it very useful to expose internal interfaces for unit test projects in order to initialize many types of objects at one time (when they all implement the same interface).  Of course, I did not want to switch over the access modifier for the interfaces every time I wanted to run unit tests, so I used the InternalsVisibleAttribute

This attribute can also be very useful for types you want to expose to your own assemblies, but not to outside customers (as it may clutter up their design environment).

First thing you will need to do is fire up the Visual Studio 2005 Command Prompt, and go to the folder for the dll or exe for which you want to grant access.

Next you want to issue: sn.exe -t myDLL.dll  (substitute myDLL with the name of your dll)

Then you can extract the public key for your dll by issuing the following: sn.exe -tp

Take the output from this command (it will be about 240 characters) and insert the following into the AssemblyInfo.cs file in the dll whose internal types you want to publish:

 [assembly: InternalsVisibleTo("mydll, PublicKey=892s000004800000940000000602000000240000525341312224000001001100e5dd149f

Voila!  Happy coding,


Tools   Print   Email   Digg   reddit Posted on Sunday, November 18, 2007 3:42 PM C# , Software Design | Back to top

Comments on this post: InternalsVisibleTo - Useful .NET Attribute...

# re: InternalsVisibleTo - Useful .NET Attribute...
Requesting Gravatar...
In addition to being useful for unit testing, this also comes in handy for domain models when you place your domain object in a separate assembly from your service components.

This allows you do expose domain object level properties and methods that can only be utilized from your service layer.

I recently posted about this, you may want to check it out at:
Left by Jason Whitehorn on Dec 02, 2007 11:41 PM

Your comment:
 (will show your gravatar)

Copyright © Jonathan Starr | Powered by: