Friend Assemblies in C#

While going through the CLR via C# book i came a across a topic Friend Assemblies, What exactly is the Friend Assemblies ?

Lets understand it by an example given in the book suppose we are Application for which we have 2 different teams named Team Alpha and Team Beta.

Team Alpha, that is defining a bunch of utility types in one assembly, and they expect these types to be used by members in another team, Team Beta.

For various reasons such as time schedules or geographical location, or perhaps different cost centers or reporting structures, these two teams cannot build all of their types into a single assembly; instead, each team produces its own assembly file.

In order for Team Beta’s assembly to use Team Alpha’s types, Team Alpha must define all of their utility types as public. However, this means that their types are publicly visible to any and all assemblies; developers in another company could write code that uses the public utility types, and this is not desirable. Maybe the utility types make certain assumptions that Team Beta ensures when they write code that uses Team Alpha’s types. What we’d like to have is a way for Team Alpha to define their types as internal while still allowing Team Beta to access the types. The CLR and C# support this via friend assemblies. This friend assembly feature is also useful when you want to have one assembly containing code that performs unit tests against the internal types within another assembly.

When an assembly is built, it can indicate other assemblies it considers “friends” by using the InternalsVisibleTo attribute defined in the System.Runtime.CompilerServices namespace.The attribute has a string parameter that identifies the friend assembly’s name and public key (the string you pass to the attribute must not include a version, culture, or processor architecture). Notethat friend assemblies can access all of an assembly’s internal types as well as these type’s internalmembers. Here is an example of how an assembly can specify two other strongly named assemblies

named “Assmblytest” and “Nirbhay” as its friend assemblies:

using System;

using System.Runtime.CompilerServices; // For InternalsVisibleTo attribute

// This assembly’s internal types can be accessed by any code written

// in the following two assemblies (regardless of version or culture):

[assembly:InternalsVisibleTo(“Assmblytest, PublicKey=12345678…90abcdef”)]

[assembly:InternalsVisibleTo(“Nirbhay, PublicKey=b77a5c56…1934e089”)]

internal sealed class SomeInternalType { … }

internal sealed class AnotherInternalType { … }

Accessing the above assembly’s internal types from a friend assembly is trivial. For example,

here’s how a friend assembly called “Assmblytest” with a public key of “12345678…90abcdef” can access the internal type SomeInternalType in the assembly above.

Published by Nirbhay

Technology Geek,Blogger,Reviewer

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 )

Google photo

You are commenting using your Google 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 )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: