【问题标题】:Embedded library in multi targeting .NET project多目标 .NET 项目中的嵌入式库
【发布时间】:2020-05-05 16:01:23
【问题描述】:

我有一个 .NET 项目,它有 2 个目标框架:netstandard1.6 和 net40。我正在尝试嵌入一个以 netstandard1.3 和 net40 为目标的库(ICU4N 和 J2N)。

我创建了一个...\lib\ 文件夹,在其中放置了对应目标的 .dll 库。

我的 .csprog 文件如下所示

<?xml version="1.0" encoding="utf-8"?>
<Project Sdk="Microsoft.NET.Sdk" ToolsVersion="15.0">
  <PropertyGroup Label="Configuration">
    <SignAssembly>True</SignAssembly>
    <DelaySign>False</DelaySign>
    <DocumentationFile>$(TargetDir)bin\$(Configuration)\$(TargetFramework)\someName.xml</DocumentationFile>
  </PropertyGroup>
  <PropertyGroup>
    <TargetFrameworks>netstandard1.6;net40</TargetFrameworks>
  </PropertyGroup>
  <PropertyGroup>
    <OutputType>library</OutputType>
  </PropertyGroup>

  ...

  <PropertyGroup>
    <NoWarn>1701;1702;1591;1570;1572;1573;1574;1580;1584;1658</NoWarn>
  </PropertyGroup>
  <ItemGroup Condition=" '$(TargetFramework)' == 'net40' ">
    <Reference Include="System" />
  </ItemGroup>

  ...

  <ItemGroup Condition=" '$(TargetFramework)' == 'netstandard1.6' ">
    <PackageReference Include="Microsoft.NETCore.Portable.Compatibility" Version="1.0.1" />

    <Reference Include="ICU4N, Version=60.0.0.0, Culture=neutral, PublicKeyToken=efb17c8e4f0e291b">
      <HintPath>lib\ICU4N\netstandard1.3\ICU4N.dll</HintPath>
    </Reference>
    <Reference Include="J2N, Version=2.0.0.0, Culture=neutral, PublicKeyToken=f39447d697a969af">
      <HintPath>lib\J2N\netstandard1.3\J2N.dll</HintPath>
    </Reference>
  </ItemGroup>

  <ItemGroup Condition=" '$(TargetFramework)' == 'net40' ">
    <Reference Include="ICU4N, Version=60.0.0.0, Culture=neutral, PublicKeyToken=efb17c8e4f0e291b">
      <HintPath>lib\ICU4N\net40\ICU4N.dll</HintPath>
    </Reference>
    <Reference Include="J2N, Version=2.0.0.0, Culture=neutral, PublicKeyToken=f39447d697a969af">
      <HintPath>lib\J2N\net40\J2N.dll</HintPath>
    </Reference>
  </ItemGroup>
</Project>

当我运行程序时,

System.IO.FileNotFoundException : Could not load file or assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

报错,出现这个错误是因为...\bin\Debug\net40\ICU4N.dll...\bin\Debug\netstandard1.6\ICU4N.dll文件相同,但应该不同。

问题是如何配置项目和 .csproj 文件,以便在构建期间 netstandard1.3 库的版本落入文件夹 ...\bin\Debug\netstandard1.6\,而 net40 版本的库落入文件夹 ...\bin\Debug\net40\,因为现在net40 版本的库位于文件夹...\bin\Debug\netstandard1.6\...\bin\Debug\net40\

提前谢谢你。

【问题讨论】:

    标签: c# .net embedded-resource csproj


    【解决方案1】:

    问题出在HintPath,第一个找到的库是嵌入的。

    决定: 为 netstanard1.6 目标块添加以下代码。

        <None Remove="lib\ICU4N\net40\ICU4N.dll" />
        <None Remove="lib\J2N\net40\J2N.dll" />
    

    【讨论】:

    • 另请注意,所有 dll 文件可能都被引用为 None 或 Content 项 - 这也会导致它们出现在 VS 解决方案资源管理器中。如果您通过 VS UI 将它们从项目中排除,这也应该有助于解决问题 - MSBuild 还考虑 None 和 Content 项目以进行程序集解析。
    • 谢谢,我会考虑的。
    猜你喜欢
    • 1970-01-01
    • 2022-08-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-08
    • 2018-05-21
    相关资源
    最近更新 更多