DCSIMG
C# Friend assemblies - Pini Dayan

Pini Dayan

The best thing about a boolean is even if you are wrong, you are only off by a bit.

C# Friend assemblies

If you ask any C# developer for the features  C# 2 has to offer the answers will be Generics, Anonymous functions , partial types and so on, That would be my answer to , but lately i needed something new which i didn't know exited in the .NET world up until now.
This new feature (well not so new :)) is called a Friend Assembly.
The use in this  feature is not very common since the need for it is not common as well.

The motivation is as follow: Suppose you are building a solution and this solution contains many assemblies,
And suppose one of these assemblies contains some types with access modifier as internal.
If you wish to use this type functionality from some other assembly , you simply cannot.
If you need this types functionality because there is,  lets say a logical group of assemblies that can share this functionality there is a solution. The solution is friend assemblies.
The solution is as follow, Lets say assembly A has the internal type we want to share to assembly B.
We need to add the following code to the assemblyinfo.cs  of assembly A:
[assembly:InternalsVisiblieTo("AssemblyB")]
where AssemblyB is the name of the friend assembly.

Some notes:
1. The relationship of these 2 or 3 assemblies is not transitive nor it is symmetric.
So if Assembly C is a friend of Assembly B and B is a friend of A that C is not a friend of A.
In addition if B is a friend of A that A is not a friend of B.

2. This attribute is defined in the System.Runtime.CompilerServices

 

Enjoy.

Posted: Nov 01 2008, 06:14 AM by Pini Dayan | with 1 comment(s)
תגים:,

Comments

Yaron Naveh said:

This can also be very useful for unit tests - grant your test assembly permission to access internal methods of the application under test.

# November 1, 2008 8:41 AM
Leave a Comment

(required) 

(required) 

(optional)

(required) 


Enter the numbers above: