<Project>
  <Target Name="AddInternalsVisibleTo" BeforeTargets="BeforeCompile">
    <!-- Add default suffix if there is no InternalsVisibleTo or InternalsVisibleToSuffix defined -->
    <ItemGroup Condition="@(InternalsVisibleToSuffix->Count()) == 0 AND @(InternalsVisibleTo->Count()) == 0">
      <InternalsVisibleToSuffix Include=".Tests" />
    </ItemGroup>

    <!-- Handle InternalsVisibleTo -->
    <ItemGroup Condition="'@(InternalsVisibleTo->Count())' &gt; 0">
      <AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
        <_Parameter1>%(InternalsVisibleTo.Identity)</_Parameter1>
      </AssemblyAttribute>
    </ItemGroup>

    <!-- Handle InternalsVisibleToSuffix -->
    <ItemGroup Condition="@(InternalsVisibleToSuffix->Count()) &gt; 0">
      <AssemblyAttribute Include="System.Runtime.CompilerServices.InternalsVisibleTo">
        <_Parameter1>$(AssemblyName)%(InternalsVisibleToSuffix.Identity)</_Parameter1>
      </AssemblyAttribute>
    </ItemGroup>
  </Target>
</Project>