August 7, 2009

Friend Assemblies

'InternalsVisibleToAttribute' indicates that all internal types in that assembly would be visible to another assembly, whose name is specified in the attribute constructor. In other words, that assembly becomes a friend assembly. The usage would be something like:
[assembly:InternalsVisibleToAttribute("MyFriendAssembly”)]
Or (for a strong-named friend)
[assembly:InternalsVisibleToAttribute("MyFriendAssembly, PublicKeyToken=45cb56a45e0a69a1")]

This entry goes in "AssemblyInfo.cs" of the assembly whose internal classes are being exposed. The assembly mentioned in the attribute is the one that is being given permission to access those internal definitions.
For example if Assembly A is a business logic assembly and assembly B is a unit test assembly requiring access to the internals of assembly A then the attribute definition is placed inside assembly A mentioning assembly B inside the attribute so that it may have the required access.

This has changed from .NET 5.0

Add the following to you target project, the one with internals that need to be exposed for unit testing:

<ItemGroup>
    <AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleToAttribute">
      <_Parameter1>UnitTestProject</_Parameter1>
    </AssemblyAttribute>
</ItemGroup>

No comments: