Reputation: 12560
I am working on a Windows C# .NET 4.5 program that utilizes several NuGet projects to reduce the amount of code I need to write (why reinvent the wheel?). One of these NuGet projects, for whatever reason, has a dependency that overrides one of MSCORLIB
's methods used elsewhere in my program.
ReSharper warns me of the ambiguous reference (and I cannot compile obviously), and I cannot for the life of me figure out how to specify the use of MSCORLIB
's method over this NuGet project dependency. I spent a couple hours googling and reading different things but cannot find a solution.
The part of my program that has the ambiguous reference error does not rely on the NuGet package's dependency in any way, so if I can just implement MSCORLIB
's method in just this spot I will be golden.
Is this even possible? I tried to explicitly add reference to MSCORLIB
to the project with ReSharper's "Use this assembly..." but selecting either one did not work, as well as the References tab in Visual Studio.
Upvotes: 1
Views: 2304
Reputation: 26783
You can resolve this using assembly aliases and extern alias
. To resolve this, you will need to edit by hand your projects .csproj file.
If you are using packages.config, look in your .csproj file for the <Reference>
element that corresponds to the project bringing in the conflicting type and assign it an alias.
<Reference Include="bouncy_castle_hmac_sha_pcl, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\BouncyCastle-PCL.1.0.0.6\lib\bouncy_castle_hmac_sha_pcl.dll</HintPath>
<Aliases>bouncy_castle</Aliases>
</Reference>
If you are using PackageReference instead, there won't be a direct <Reference>
element, so instead, you need to add a target to assign this value.
<Target Name="AssignAliasesToAssemblies" BeforeTargets="FindReferenceAssembliesForReferences;ResolveReferences">
<ItemGroup>
<ReferencePath Condition="'%(FileName)' == 'bouncy_castle_hmac_sha_pcl'">
<Aliases>bouncy_castle</Aliases>
</ReferencePath>
</ItemGroup>
</Target>
Then, you can reload your project. In your C#, add extern alias bouncy_castle;
to the top of the file. This will instruct the compiler how to disambiguate between the two types.
extern alias bouncy_castle;
using System.Security.Cryptography;
namespace ClassLibrary2
{
public class Class1
{
public HMACSHA1 Algorithm { get; }
public bouncy_castle::System.Security.Cryptography.HMACSHA1 TheOtherOne { get; }
}
}
By the way, see this issue: https://github.com/NuGet/Home/issues/4989
Upvotes: 2