在.NET Core 1.1 CSPROJ中引用.NET 4.5 DLL? [英] Reference .NET 4.5 dll in .NET Core 1.1 csproj?

查看:226
本文介绍了在.NET Core 1.1 CSPROJ中引用.NET 4.5 DLL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在运行VS 2017 RC4.

I'm running VS 2017 RC4.

我在.NET Core应用程序中向.NET 4.5 dll添加了引用,并且该引用可以编译.当在运行时调用引用dll的行时,我得到:

I add a reference in my .NET Core app to my .NET 4.5 dll and it compiles. When a line that references the dll is called at runtime, I get:

System.IO.FileNotFoundException: 'Could not load file or assembly 'System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'. The system cannot find the file specified.'

此图显示,要使用4.5引用,我需要使用netstandard 1.1. https://msdnshared.blob.core.windows.net/media/2016/07/172.png

This image shows that to use 4.5 references, I need to use netstandard 1.1. https://msdnshared.blob.core.windows.net/media/2016/07/172.png

假设这是我的需要,如何在.csproj中引用它?我只能找到有关何时使用project.json的旧文档.

Assuming that is what I need, how do I reference it in my .csproj? I can only find old documentation for when project.json was used instead.

我尝试添加以下内容,但没有帮助:

I tried adding the below but it did not help:

<NetStandardImplicitPackageVersion>1.1</NetStandardImplicitPackageVersion>

此外,我需要添加:

<RuntimeFrameworkVersion>1.0.3</RuntimeFrameworkVersion>

或者我得到FileNotFoundException:无法加载文件或程序集.系统找不到指定的文件.

Or I get FileNotFoundException: Could not load file or assembly. The system cannot find the file specified.

那是为什么?

以下是我的.csproj的相关部分:

Here are the relevant parts of my .csproj:

<PropertyGroup>
    <TargetFramework>netcoreapp1.1</TargetFramework>
    <PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback>
<RuntimeFrameworkVersion>1.0.3</RuntimeFrameworkVersion>
  </PropertyGroup>
<ItemGroup>
    <Reference Include="My4.5dll">
      <HintPath>Dlls\My4.5dll.dll</HintPath>
    </Reference>
  </ItemGroup>

推荐答案

您不能(安全地)将.NET Framework 4.5库加载到.NET Core中,因为它可能会使用.NET Core中不可用的API.

You can't (safely) load a .NET Framework 4.5 library into .NET Core, because it may use APIs which are unavailable in .NET Core.

仅当您的库针对portable-net45+win8(.NET Framework 4.5 和Windows 8可移植类配置文件或更高版本)时,它才可以与.NET Core一起使用.因为此特定的PCL配置文件限制了与.NET Core所基于的API(以前称为WinRT)System.Runtime兼容,所以.

Only if your library targets portable-net45+win8 (.NET Framework 4.5 and Windows 8 Portable Class Profile or higher) it can be used with .NET Core. Because this specific PCL Profile limits the API which is compatible to (what was formerly called WinRT) System.Runtime, which is what is .NET Core is based on.

有关兼容PCL配置文件的列表,请参见

For a list of compatible PCL profiles, see this list (PCL Compatibility at the bottom)

如果要引用的程序集不支持netstandard1.x或任何受支持的配置文件,则必须以.NET Framework 4.5而不是.NET Core为目标.

If the assembly you want to reference do not support netstandard1.x or any of the supported profiles, you have to target .NET Framework 4.5 instead of .NET Core.

在您的csproj中

<TargetFramework>net45</TargetFramework>
...
<ItemGroup>
    <PackageReference Include="Net45DependencyHere" Version="4.5.0" />

或者如果您有多个目标

<TargetFrameworks>net45;netcoreapp1.1</TargetFrameworks>
...
<ItemGroup>
    <PackageReference Condition="'$(TargetFramework)' == 'net45' Include="Net45DependencyHere" Version="4.5.0" />
    <PackageReference Condition="'$(TargetFramework)' == 'netcoreapp1.1' Include="NetCoreReplacementLibrary" Version="1.1.0" />

您只是无法自动魔术地使用.NET Core项目中的任何.NET Framework 4.5库.只有PCL和netstandard1.x的.

You just can't auto-magically use any .NET Framework 4.5 library in your .NET Core project. Only PCL and netstandard1.x ones.

出于完整性考虑:

如果您确定您的类库/程序包以兼容的PCL为目标,则即使它们不以netstandard1.x为目标,您也可以使nuget还原此程序包.

If you are certain that your class library/package targets a compatible PCL, you can make nuget restore this packages too, even if they don't target netstandard1.x.

<PropertyGroup>
    <PackageTargetFallback>$(PackageTargetFallback);portable-net45+win8+wp8+wpa81;</PackageTargetFallback>
</PropertyGroup>

警告语

从不,我重复一次,从不在此放置除兼容的PCL库以外的任何其他内容. 从不在此处放置net45.这只会强制NuGet下载并安装此软件包,但不会使其运行并在运行时崩溃,并出现类似上面的错误!

Word of warning

Never, I repeat, NEVER put anything else there except for compatible PCL Libraries. NEVER put net45 in here. This will just force NuGet to download and install this package, but it won't make it work and crash at runtime with similar error like you have above!

只有在这种情况下,强制nuget安装在过渡期内可以与.NET Core一起使用的软件包,直到大多数软件包都以netstandard1.x为目标!

It's only there to force nuget to install package which are know to work with .NET Core for the transition period until most packages target netstandard1.x!

这篇关于在.NET Core 1.1 CSPROJ中引用.NET 4.5 DLL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆