[英]Overriding a NuGet solution's DLL's reference to a core library
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?). 我正在使用Windows C#.NET 4.5程序,该程序利用了多个NuGet项目来减少我需要编写的代码量(为什么要重新发明轮子?)。 One of these NuGet projects, for whatever reason, has a dependency that overrides one of
MSCORLIB
's methods used elsewhere in my program. 无论出于何种原因,这些NuGet项目之一都具有一个依赖关系,该依赖关系会覆盖程序中其他地方使用的
MSCORLIB
方法之一。
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. ReSharper向我警告该引用含糊不清(而且我显然不能编译),而且我一生也无法弄清楚如何在此NuGet项目依赖项上指定使用
MSCORLIB
的方法。 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. 我的程序中具有模棱两可的引用错误的部分以任何方式都不依赖于NuGet包的依赖关系,因此,如果我可以仅在这一点上实现
MSCORLIB
的方法,那我会很高兴。
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. 我试图使用ReSharper的“使用此程序集...”
MSCORLIB
引用显式添加到项目中,但是选择其中一个都不起作用,以及Visual Studio中的“引用”选项卡。
You can resolve this using assembly aliases and extern alias
. 您可以使用程序集别名和
extern alias
解决此问题。 To resolve this, you will need to edit by hand your projects .csproj file. 要解决此问题,您将需要手动编辑项目.csproj文件。
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. 如果正在使用packages.config,请在.csproj文件中查找与引入冲突类型的项目相对应的
<Reference>
元素,并为其分配别名。
<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. 如果您改用PackageReference,则不会有直接的
<Reference>
元素,因此,您需要添加一个目标来分配该值。
<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;
在您的C#中,添加
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 顺便说一下,看到这个问题: https : //github.com/NuGet/Home/issues/4989
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.