C#: Expose internal members to outside assemblies

June 19, 2010 by C#  

Normally when members (properties/methods/delegates/indexers etc) are defined as internal, they're only available within the assembly they reside in.

internal class b
    public void c()
Note: The default modifier for a top-level class is internal e.g. omitting the internal (in the preceding snippet) means the class will be internal in anycase.

But since .net 2.0 its possible to expose internal members to assemblies outside the residing assembly using the InternalsVisibleTo attribute.

Within the assembly that we wish to expose internal members, you will need to register the names of the assembly(ies) that require access to the internals like this:
[assembly: InternalsVisibleTo("someassembly")]
[assembly: InternalsVisibleTo("someotherassembly")]
The preceding snippet can be placed within your AssemblyInfo.cs file, or within other source files.

It is also possible (and advisable) to sign the assemblies with a strong key to make it difficult for a malicious user to spoof (replace/inject) code into your assembly.

To create/assign a strong key within visual studio:
  • right click on your project
  • select properties
  • click on the signing tab
  • tick sign the assembly
  • select new(or browse for an exiting key)
In order to extract a public key, go to your visual studio command prompt, browse/point to your strong keys with the following commands:
rem extract public key
sn -p keyPair.snk publicKey.snk
rem display public key
sn -tp publicKey.snk

Next add the public key to the assembly exposing internal members:

[assembly: InternalsVisibleTo("somesignedassembly, PublicKey=0024000004800000940000000602000000240000525341310004000001000100b12297f91269c8957718971524fc11c1eb8d62bba5b04b82149ca45f4567a6f6f12a76db30fd3a63f89066137b331e48b8e8ff9753720ca1acf4e0910edd606654c8db9b2d5cd5ca08fc4fe7c2fe3d8bcc9debdb292cd0dedfe0737170a539065df87b77a52376b2588e8a1d12a91650aaf0db0204e875b4f40821c714b8e3a6")]

Note: In order to use a signed assembly, both the target and source assemblies need to be signed, in order to use an unsigned assembly, both target and source assemblies need to be unsigned.

Some sources:

Strong Name Tool (Sn.exe)
InternalsVisibleToAttribute Class

Leave a Comment

Latest Posts

MTN targeting 'freeloading' WhatsApp

January 19, 2016

Personal Movie Database Part 2 - Service (PHP)

January 15, 2016

Paris Terror Attacks

November 14, 2015

Personal Movie Database Part 1 - UI Overview (JQuery Mobile AngularJS)

November 10, 2015

JavaScript Tip - Checking for undefined

April 9, 2015

Simple JavaScript Shop - Back to the future : Part 1

March 20, 2015

Getting my hands dirty with TypeScript

February 17, 2015

Quick and dirty look at TypeScript

January 29, 2015

My personal life in review

January 21, 2015

Quick rundown of possible new C# 6.0 features (perhaps, maybe - I think)

January 19, 2015

Most Commented on Posts

Moving items between listboxes in ASP.net/PHP example

Move items between two listboxes in ASP.net(C#, VB.NET) and PHP
June 12, 2008

Simple WYSIWYG Editor

Creating a WYSIWYG textbox for your website is actually quite simple.
February 1, 2007

C# YouTube : Google API

Post on how to integrate with YouTube using the Google Data API
March 12, 2011

Populate a TreeView Control C#

Populate a TreeView control in a windows application.
August 27, 2009

ASP.NET(C#) : Autocomplete TextBox - Part 1 (From Scratch)

Part one of how to create an autocomplete textbox using ASP.NET(C#)
March 27, 2011

Angular   ASP.NET   Astronomy   Breaking News   C#   C++   Coding Horrors   Comedy   Cross Browser   Design Patterns   Google API   IIS   Integration   JavaScript   Jquery Mobile   Love and War   Microsoft Office   Personal   PHP   SEO   South Africa   SQL   Threading   TypeScript   Visual Basic   XML