Allowing Your Assmbly to Properly Declare it’s Public Interfaces

I have been looking at Enterprise Library 3.0 more this weekend and discovered something I thought was noteworthy.
 
If you have an assembly that has members that you want available to another assembly (say, a test assembly), but don’t want have all your internal types visible to other client assemblies, you can use the InternalsVisibleToAttribute in your AssemblyInfo.cs.
 
A common complaint about unit testing is that you have to make everything public and accessible, possibly breaking your intended design for the sake of testability. You can correct that issue by adding this attribute to your target assembly’s AssemblyInfo.cs file:
 
[assembly: InternalsVisibleTo("MyTestAssembly")]
 
If the assembly that you are trying to make the internals visible from is signed with a strong-name key, you need to specify the PublicKey of the strong-name key that you signed your assembly with.
 
To find out the Public Key of your assembly you can issue the following commands:
 
sn.exe -p MyKey.snk mykey.pub
 
sn.exe -tp mykey.pub
 
Then  you can assign the PublicKey value in the InternalsVisibleToAttribute as follows:
 
[assembly: InternalsVisibleTo("MyTestAssembly, PublicKey=

123240123454800000940000000602000000240000525341310004000001000100130bcc959a5a8cf841e2b6d1e9aa22d1a334ec595a1641561f1ad4477ba5f145ef4977ea59cbd026f09a2084a2ffc2de5612e7f8136f65864a56909c473832e70e5b36827881aeac9f971ad3ec810dccaccd624ad584295e3eadfdaa319fd23a57d9ca71d8b25b0165f96deda5f1fea669719fe70f202a085fa04c0d14b016bd")]

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s