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

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

问题描述

我有一个简单的 .Net Core 控制台应用程序在同一解决方案中引用了 .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:在未引用的程序集中定义了类型对象".您必须添加对程序集 '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'.

我是否试图做一些不允许的事情?

Am I trying to do something that is not allowed?

是否有某种方法可以从 .Net Core 应用程序中引用 .Net Framework 4.6(或 Mono)程序集(或 P/Invoke)?

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 Standard"的 .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天全站免登陆