Sunday, November 18, 2007

Tip of the day: Friend assemblies

The internal keyword (C#) is defined as the way to restrict access to members only to types in same assembly.

But, since the .net framework 2.0, there is a way to to define an assembly as a friend assembly. This can be done by specifying

[assembly:InternalsVisibleTo("friend assembly")]

in the AssemblyInfo.cs file (or in any other file).


After specifying this, the friend assembly can access the internals of the assembly marked with this attribute.


I can see two useful thing that can be achieved:


1) Better encapsulation of course.


2) Enable access for unit testing by using this strategy.


The msdn documentation is here.

No comments: