Friend Assembly

Why Trust Techopedia

What Does Friend Assembly Mean?

A friend assembly, in C#, refers to the .NET assembly that can access the members of another assembly. Members in an assembly that are accessed from friend assembly need to be marked as
internal.

Advertisements

Friend assemblies are used in scenarios where there is a need to access internal classes and members in one assembly from another specific assembly. It enables two specific assemblies to share members or functionality without making them public.

Friend assemblies are also used in unit testing where the assembly underlying the test code needs to access the members which are marked as internal in the assembly to be tested. It is useful in development of class libraries that require additions contained in separate assemblies which need to access members in existing assemblies.

Techopedia Explains Friend Assembly

By marking members of a class as internal, they are visible to other classes within the same assembly but are private for classes that reside outside the assembly. To access the internal members from a friend assembly, name of friend assembly should be specified with the attribute, InternalsVisibleToAttribute in the assembly where the internal members are defined. Friend assembly cannot access private types and private members.

For example, InternalsVisibleToAttribute in Assembly X can be used to specify Assembly Y as friend assembly, so that Assembly Y can access all types and members of Assembly X that are marked as internal.

Following are the guidelines related to development of friend assemblies:

  • While using signed assemblies , the tool, ‘sn.exe’ can be used to get the public key that has to be passed to the attribute specifying friend assembly
  • While compiling a friend assembly that accesses internal members of another assembly, the compiler option (/out) has to be set with the name of the output file (.exe or .dll)
  • It is necessary to explicitly specify every assembly that needs to be friend for the assembly to which it needs access
  • It is suggested to use friend assemblies without breaking the rules of encapsulation
Advertisements
Margaret Rouse
Technology expert
Margaret Rouse
Technology expert

Margaret is an award-winning writer and educator known for her ability to explain complex technical topics to a non-technical business audience. Over the past twenty years, her IT definitions have been published by Que in an encyclopedia of technology terms and cited in articles in the New York Times, Time Magazine, USA Today, ZDNet, PC Magazine, and Discovery Magazine. She joined Techopedia in 2011. Margaret’s idea of ​​a fun day is to help IT and business professionals to learn to speak each other’s highly specialized languages.