Using InternalsVisibleTo Attribute with Strong Named Assemblies

The InternalsVisibleTo Attribute can be used to allow another assembly (such as your Unit Test assembly) to see the internal members of your assembly.  I prefer this method to either putting unit tests in my primary assembly or to using the VSTS feature of autogenerating accessors to my private members.  (Those are real fun when you go to refactor a project…)

Now most of the info you will find about this refers to the earlier beta usage.  The attribute used to take in a PublicKeyToken but now takes in a PublicKey.

To use the InternalsVisibleTo Attribute:

1.  Run sn.exe -p MyStrongNameKey.snk MyStrongNameKey.PublicKey

This will extract to public key to a file with the .PublicKey extension.  (I hate using the .pub extension because it is seen as a Microsoft Publisher file…)

2.  run sn.exe -tp MyStrongNameKey.PublicKey

This will display your public key for you.  Copy this key. 

3.  Create an attribute as such:

[assembly: InternalsVisibleTo( "MyProject.UnitTests, PublicKey=PASTE-YOUR-PUBLIC-KEY-HERE" )]

4.  Note that the public key that was displayed was 5 lines or so.  Remove the line breaks from the public key and paste it into your attribute.

5.  You are done!  The assembly MyProject.UnitTests can now see the internal members of your assembly.

 

Brenton House

Published Monday, April 17, 2006 8:11 AM by dotnetboy2003
Filed under: ,

Comments

# Jason Haley - Interesting Finds

Jason Haley - Interesting Finds

Tuesday, April 18, 2006 9:27 AM by TrackBack

# Buspar.

Buspar. Buspar and modafinil together.

Thursday, November 13, 2008 10:28 PM by Ic lorazepam buspar.

# re: Using InternalsVisibleTo Attribute with Strong Named Assemblies

Thanks, this really helped me to understand what I was doing wrong.

Friday, May 08, 2009 5:33 AM by Stefano

# Making Internals Available to Other Assemblies « LP on .NET

Pingback from  Making Internals Available to Other Assemblies « LP on .NET

Wednesday, October 14, 2009 7:35 AM by Making Internals Available to Other Assemblies « LP on .NET

# InternalsVisibleToAttribute und signierte Assemblies: So funktionierts garantiert.

Stellt euch vor, ihr habt zwei Assemblies: MyProject.Core MyProject.Core.Tests Im Core-Projekt gibt es

Friday, December 04, 2009 10:44 AM by Peter Bucher

# Strong signing of compiled assemblies | The Full Stack

Pingback from  Strong signing of compiled assemblies | The Full Stack

Tuesday, May 14, 2013 11:28 AM by Strong signing of compiled assemblies | The Full Stack