将 .net 核心与旧的 .net 框架 dll 一起使用 [英] Use .net core with legacy .net framework dlls
问题描述
我可以将 .net core 与旧的 .net framework dll 一起使用吗?答案似乎是否定的...但我只能找到引用 project.json 的资源,该文件不再存在.
Can I use .net core with legacy .net framework dlls? The answer seems to be no... but I can only find resources referring to project.json, which doesn't exist anymore.
我创建了一个新的 .net 核心库并尝试引用旧的 .net 框架 DLL.当我尝试调用 DLL 时,vs2017 抱怨我没有正在寻找的 Stream 对象.
I created a new .net core library and tried to reference a legacy .net framework DLL. When I tried to call into the DLL, vs2017 complained that I didn't have the Stream object is was looking for.
它建议我引用 mscorlib.dll 或 installa Nuget 包.
It suggested I reference either mscorlib.dll or installa Nuget package.
快速帮助未能引用 mscorlib.dll.如果我手动引用它,则会出现以下错误:
The quick help failed to reference mscorlib.dll. If I manually referenced it, I get the following error:
类型 'TargetFrameworkAttribute' 存在于 'mscorlib,版本=4.0.0.0,文化=中性,PublicKeyToken=b77a5c561934e089' 和'System.Runtime,版本=4.1.0.0,文化=中性,PublicKeyToken=b03f5f7f11d50a3a' C:Users...AppDataLocalTemp.NETCoreApp,Version=v1.1.AssemblyAttributes.cs
The type 'TargetFrameworkAttribute' exists in both 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089' and 'System.Runtime, Version=4.1.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a' C:Users...AppDataLocalTemp.NETCoreApp,Version=v1.1.AssemblyAttributes.cs
NuGet 包是 Microsoft.NETFx2.0.快速帮助无法安装它.如果我从命令行运行它:
The NuGet package is Microsoft.NETFx2.0. The quick help fails to install it. If I run it from the command line:
> PM> install-package microsoft.netfx20 GET
> https://api.nuget.org/v3/registration2-gz/microsoft.netfx20/index.json
> OK
> https://api.nuget.org/v3/registration2-gz/microsoft.netfx20/index.json
> 46ms Restoring packages for ... Install-Package : Package
> Microsoft.NetFX20 1.0.3 is not compatible with netcoreapp1.1
> (.NETCoreApp,Version=v1.1). Package Microsoft.NetFX20 1.0.3 supports:
> net20 (.NETFramework,Version=v2.0)At line:1 char:1
> + install-package microsoft.netfx20
> + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> + CategoryInfo : NotSpecified: (:) [Install-Package], Exception
> + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand
> Install-Package : One or more packages are incompatible with
> .NETCoreApp,Version=v1.1.At line:1 char:1
> + install-package microsoft.netfx20
> + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> + CategoryInfo : NotSpecified: (:) [Install-Package], Exception
> + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand
> Install-Package : Package restore failed. Rolling back package changes
> for .At line:1 char:1
> + install-package microsoft.netfx20
> + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
> + CategoryInfo : NotSpecified: (:) [Install-Package], Exception
> + FullyQualifiedErrorId : NuGetCmdletUnhandledException,NuGet.PackageManagement.PowerShellCmdlets.InstallPackageCommand
> Time Elapsed: 00:00:00.8035644
推荐答案
困难的话题.通常 .NET Framework 和 .NET Core 是不兼容的.它们针对一组不同的程序集(mscorlib 与 System.Runtime),这会导致不兼容,因为所有类型的用法都以类型来自的程序集为前缀.
Difficult topic. Generally .NET Framework and .NET Core are incompatible. They target a different set of assemblies (mscorlib vs. System.Runtime) which causes incompatibilities since all usages of types are prefixed with the assembly the type is from.
从 .NET Core 2(目前为预览版)开始,您可以通过不可见的兼容性垫片引用 .NET Framework 程序集.这允许您引用程序集并成功编译.
Starting with .NET Core 2 (currently in preview), you can reference .NET Framework assemblies through an invisible compatibility shim. This allows you to reference the assembly and compile successfully.
它不能保证应用程序会成功运行,因为 .NET Core 不提供 .NET Framework 的所有 API.如果是这种情况,您将在运行时收到 PlatformNotSupportedException
或 MissingTypeException
和朋友.
It doesn't guarantee though that the application will run successfully, since .NET Core doesn't provide all the APIs from .NET Framework. You'll get PlatformNotSupportedException
or MissingTypeException
and friends at runtime if that's the case.
这篇关于将 .net 核心与旧的 .net 框架 dll 一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!