简体   繁体   English

覆盖NuGet解决方案的DLL对核心库的引用

[英]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.

相关问题 Nuget库-将库的DLL复制到当前项目的输出目录 - Nuget Library - Copy DLL of Library to current project's output directory dll和nuget包之间有什么区别? - What's the difference between a dll and nuget package? 具有NuGet依赖项的参考DLL - Reference DLL with NuGet dependencies 是否可以在另一个解决方案中从 ASP.NET Webforms 项目中引用 ASP.NET Core 2.0 类库 dll? - Possible to reference an ASP.NET Core 2.0 class library dll from an ASP.NET Webforms project in another solution? 具有自己的NuGet依赖项时如何从Full .Net引用标准库 - How to reference Standard library from Full .Net when it have it's own NuGet dependencies 流利的NHibernate解决方案结构-如何在Web App / Console App后端引用一次DLL - Fluent NHibernate Solution Structure - How to reference DLL's once in back end of Web App / Console App 在寻求新的工作空间或分支之后,解决方案中缺少参考dll - Reference dll 's are missing from the solution after going for new work space or branching NuGet Package 无法找到并使用它自己的 DLL - NuGet Package can't find and use it's own DLL's Nuget Library.exe 未出现在主项目中 - Nuget Library .exe's are not Appearing In Main Project 是否可以引用解决方案中存在的项目,如果不在.NET Core中,可以使用NuGet程序包引用作为后备吗? - Is it possible to reference a project that exists in the solution and use NuGet package reference as fall-back if not in .NET Core?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM