Definition - 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
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