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 myDLL.pub  (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 mykey.pub

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
744f5928e87037e26c6f9a9154bcdb5bceee4041c119c0d500d559780596c6305bb1025392b1502558
cb5b468f13c46acf363619115bdd866835f95ddba349343483df54540dd49437a37699c6aafe42fdbc
c391a65907d38768c80c2b9c15cbc55d7ccca9aaa1713476910a1169c93a566541e5ba67b371920053
c8"]  

Voila!  Happy coding,

Jonathan

Tools   Print   Email   Del.icio.us   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:
http://geekswithblogs.net/jwhitehorn/archive/2007/11/09/116750.aspx
Left by Jason Whitehorn on Dec 02, 2007 11:41 PM

Your comment:
 (will show your gravatar)


Copyright © Jonathan Starr | Powered by: GeeksWithBlogs.net