TL;DR: Use the PackageReference's Aliases
attribute in *.csproj
and add the alias to the affected *.cs
files:
<PackageReference Include="PackageAffectedByConflict" Aliases="AltGlobalNamespace" />
extern alias AltGlobalNamespace;
using AltGlobalNamespace.ConflictedName;
According to the NuGet documentation from Microsoft:
In some rare instances different packages will contain classes in the same namespace. Starting with NuGet 5.7 & Visual Studio 2019 Update 7, equivalent to ProjectReference, PackageReference supports Aliases. By default no aliases are provided. When an alias is specified, all assemblies coming from the annotated package with need to be referenced with an alias.
The documentation also links an example on GitHub showing how to use the Aliases
attribute:
PackageReferenceAliasesExample.csproj
:
<Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="NuGet.Versioning" Version="5.8.0" Aliases="ExampleAlias" />
</ItemGroup>
</Project>
Program.cs
:
extern alias ExampleAlias;
using System;
namespace PackageReferenceAliasesExample
{
class Program
{
static void Main(string[] args)
{
var version = ExampleAlias.NuGet.Versioning.NuGetVersion.Parse("5.0.0");
Console.WriteLine($"Version : {version}");
}
}
}