从.Net Core应用程序调用.Net Framework 4(或Mono)程序集 [英] Calling a .Net Framework 4 (or Mono) assembly from a .Net Core application

查看:854
本文介绍了从.Net Core应用程序调用.Net Framework 4(或Mono)程序集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个简单的.Net Core Console应用程序,在同一解决方案中引用了.Net Framework 4.6.2项目。生成时,出现以下错误:

I have a simple .Net Core Console application referencing a .Net Framework 4.6.2 project in the same solution. When I build, I get the following error:


错误CS0012:类型'Object'是在未引用的程序集中定义的。您必须添加对程序集'mscorlib,Version = 4.0.0.0,Culture = neutral,PublicKeyToken = b77a5c561934e089'的引用。

error CS0012: The type 'Object' is defined in an assembly that is not referenced. You must add a reference to assembly 'mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089'.

是否可以通过某种方式引用.Net Framework 4.6(或Mono)程序集(或P /调用它)。

Is there some way to reference a .Net Framework 4.6 (or Mono) assembly (or P/Invoke it) from a .Net Core application?

使用Visual Studio 2017,.Net Core 1.1

Using Visual Studio 2017, .Net Core 1.1

推荐答案

在.NET Core 1.x中,不允许您引用.NET Framework 4.6(或Mono)程序集。 .NET Core 1.x和.NET Framework是2个不同的运行时,它们的类型不会混合-就像生成错误消息中所述。

In .NET Core 1.x, you are not allowed to reference a .NET Framework 4.6 (or Mono) assembly. .NET Core 1.x and .NET Framework are 2 different runtimes, and their types don't mix - like the build error message says.

相反,您可以这样做是为了重新编译针对 .NET标准的.NET Framework 4.6程序集。 了解有关.NET Standard的更多信息。为.NET Standard编译库允许该库在.NET Framework,Mono或.NET Core上运行。另一个选择是将.NET Framework 4.6程序集多目标化,因此它既可以为.NET Framework 4.6也可以为.NET Core 1.1构建。只有.NET Standard不支持您的库所需的API时,才应执行此操作。这与@VMAtm的答案大致相同,但是您可以在VS 2017中使用单个.csproj来做到这一点。

Instead, what you can do is to recompile that .NET Framework 4.6 assembly targeting ".NET Standard". Read more about .NET Standard. Compiling a library for .NET Standard allows that library to run on .NET Framework, Mono, or .NET Core. Another option is to multi-target that .NET Framework 4.6 assembly, so it builds both for .NET Framework 4.6 and for .NET Core 1.1. You should only do this if the APIs your library requires are not supported by .NET Standard. This is roughly the same as @VMAtm's answer, but you can do it with a single .csproj in VS 2017.

好消息是,您正在尝试做什么.NET Core 2.0上将支持该版本,该版本计划在以后发布今年

The good news is, what you are trying to do will be supported on .NET Core 2.0, which is slated for release later this year.

这篇关于从.Net Core应用程序调用.Net Framework 4(或Mono)程序集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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