如何在WinForms中引用.NET Core库-或.NET Standard解释

I got an interesting email today. The author said "I have a problem consuming a .net core class library in a winforms project and can't seem to find a solution." This was interesting for a few reasons. First, it's solvable, second, it's common, and third, it's a good opportunity to clear a few things up with a good example.

我今天收到一封有趣的电子邮件。 作者说:“我在Winforms项目中使用.net核心类库时遇到问题,似乎找不到解决方案。” 出于几个原因,这很有趣。 首先,它是可解决的,其次,它是常见的,其次,这是一个很好的机会,以一个很好的例子来说明一些问题。

To start, I emailed back with "precision questioning." I needed to assert my assumptions and get a few very specific details to make sure this was, in fact, possible. I said. "What library are you trying to use? What versions of each side (core and winforms)? What VS version?"

首先,我通过电子邮件发送“精确询问”。 我需要断言自己的假设并获得一些非常具体的细节,以确保实际上是可能的。 我说。 “您想使用什么库?每面的哪个版本(核心和winforms)?哪个VS版本?”

The answer was "I am working with VS2017. The class library is on NETCoreApp 1.1 and the app is a Winforms project on .NET Framework 4.6.2."

答案是“我正在使用VS2017。类库位于NETCoreApp 1.1上,而该应用程序是.NET Framework 4.6.2上的Winforms项目。”

Cool! Let's solve it.

凉! 让我们解决它。

从WinForms引用.NET Core库(运行.NET Full Framework) (Referencing a .NET Core library from WinForms (running .NET Full Framework))

Before we parse this question. Let's level-set.

在我们解析这个问题之前。 让我们进行水平设定。

.NET is this big name. It's the name for the whole ecosystem, but it's overloaded in such a way that someone can say "I'm using .NET" and you only have a general idea of what that means. Are you using it on mobile? in docker? on windows?

.NET是这个大名。 它是整个生态系统的名称,但是它以某种方式过载,以至于有人可以说“我正在使用.NET”,而您对它的含义只有一个大致的了解。 您在手机上使用它吗? 在码头工人? 在窗户上?

Let's consider that ".NET" as a name is overloaded and note that there are a few "instances of .NET"

让我们考虑“ .NET”作为重载,并注意有一些“ .NET实例”

  • .NET (full) Framework - Ships with Windows. Runs ASP.NET, WPF, WinForms, and a TON of apps on Windows. Lots of businesses depend on it and have for a decade. Super powerful. Non-technical parent maybe downloads it if they want to run paint.net or a game.

    .NET(完整)框架-Windows附带。 在Windows上运行ASP.NET,WPF,WinForms和大量应用程序。 许多企业都依赖它,并且已经有十年了。 超级强大。 非技术父母如果要运行paint.net或游戏,可以下载该文件。

  • .NET Core - Small, fast, open source, and cross-platform. Runs not only on Windows but also Mac and a dozen flavors of Linux.

    .NET Core-小型,快速,开放源代码和跨平台。 不仅可以在Windows上运行,而且可以在Mac和十几种Linux上运行。

  • Xamarin/Mono/Unity - The .NET that makes it possible to write apps in C# or F# and run them on everything from an iPad to cheap Android phone to a Nintendo Switch.

    Xamarin / Mono / Unity-使用.NET可以用C#或F#编写应用程序,并在从iPad到便宜的Android手机再到Nintendo Switch的所有设备上运行它们。

All of these runtimes are .NET. If you learn C# or F# or VB, you're a .NET Programmer. If you do a little research and google around you can write code for Windows, Mac, Linux, Xbox, Playstation, Raspberry Pi, Android, iOS, and on and on. You can run apps on Azure, GCP, AWS - anywhere.

所有这些运行时都是.NET。 如果您学习C#或F#或VB,则说明您是.NET程序员。 如果您进行了一些研究,并且可以在Google周围搜索,则可以为Windows,Mac,Linux,Xbox,Playstation,Raspberry Pi,Android,iOS等编写代码。 您可以在任何地方的Azure,GCP,AWS上运行应用程序。

什么是.NET标准? (What's .NET Standard?)

.NET Standard isn't a runtime. It's not something you can install. It's not an "instance of .NET."  .NET Standard is an interface - a versioned list of APIs that you can call. Each newer version of .NET Standard adds more APIs but leaves older platforms/operating systems behind.

.NET Standard不是运行时。 这不是您可以安装的东西。 它不是“ .NET实例”。 .NET Standard是一个接口-您可以调用的API版本列表。 .NET Standard的每个较新版本都添加了更多API,但将较旧的平台/操作系统留在了后面。

The runtimes then implement this standard. If someone comes out with a new .NET that runs on a device I've never heard of, BUT it "implements .NET Standard" then I just learned I can write code for it. I can even use my existing .NET Standard libraries. You can see the full spread of .NET Standard versions to supported runtimes in this table.

然后,运行时将实施此标准。 如果有人推出了一个在我从未听说过的设备上运行的新.NET,但它“实现了.NET Standard”,那么我才知道我可以为此编写代码。 我什至可以使用现有的.NET Standard库。 您可以在此表中看到.NET Standard版本到受支持的运行时的完整分布

Now, you could target a runtime - a specific .NET - or you can be more flexible and target .NET Standard. Why lock yourself down to a single operating system or specific version of .NET? Why not target a list of APIs that are supported on a ton of platforms?

现在,您可以将运行时(特定于.NET)作为目标,或者可以更灵活地针对.NET Standard。 为什么将自己锁定在单个操作系统或特定版本的.NET? 为什么不针对众多平台所支持的API列表呢?

The person who emailed me wanted to "run a .NET Core Library on WinForms." Tease apart that statement. What they really want is to reuse code - a dll/library specifically.

给我发送电子邮件的人想要“在WinForms上运行.NET Core库”。 逗弄那句话。 他们真正想要的是重用代码-专门用于dll /库。

When you make a new library in Visual Studio 2017 you get these choices. If you're making a brand new library that you might want to use in more than one place, you'll almost always want to choose .NET Standard.

在Visual Studio 2017中创建新库时,会得到这些选择。 如果要制作一个可能要在多个地方使用的全新库,则几乎总是要选择.NET Standard。

.NET Standard isn't a runtime or a platform. It's not an operating system choice. .NET Standard is a bunch of APIs.

.NET Standard不是运行时或平台。 这不是操作系统的选择。 .NET Standard是一堆API。

Pick .NET Standard

Next, check properties and decide what version of .NET Standard you need.

接下来,检查属性并确定所需的.NET Standard版本。

What version of .NET Standard?

The .NET Core docs are really quite good, and the API browser is awesome. You can find them at https://docs.microsoft.com/dotnet/ 

.NET Core文档确实非常好,并且API浏览器很棒。 您可以在https://docs.microsoft.com/dotnet/上找到它们

The API browser has all the .NET Standard APIs versioned. You can put the version in the URL if you like, or use this nice interface. https://docs.microsoft.com/en-us/dotnet/api/?view=netstandard-2.0

API浏览器具有所有.NET Standard API版本。 您可以根据需要将版本放在URL中,或使用此漂亮的界面。 https://docs.microsoft.com/zh-cn/dotnet/api/?view=netstandard-2.0

You can check out .NET Standard 1.6, for example, and see all the namespaces and methods it supports. It works on Windows 10, .NET Framework 4.6.1 and more. If you need to make a library that works on Windows 8 or an older .NET Framework like 4.5, you'll need to choose a lower .NET Standard version. The table of supported platforms is here.

例如,您可以签出.NET Standard 1.6 ,并查看它支持的所有名称空间和方法。 它适用于Windows 10,.NET Framework 4.6.1及更高版本。 如果您需要制作一个可在Windows 8或更旧的.NET Framework(例如4.5)上运行的库,则需要选择一个较低的.NET Standard版本。 支持的平台表在这里

From the docs - When choosing a .NET Standard version, you should consider this trade-off:

从文档开始-选择.NET Standard版本时,您应该考虑以下折衷:

  • The higher the version, the more APIs are available to you.

    版本越高,向您提供的API越多。
  • The lower the version, the more platforms implement it.

    版本越低,实现的平台越多。

In general, we recommend you to target the lowest version of .NET Standard possible. The goal here is reuse. You can also check out the Portability Analyzer and run it on your existing libraries to see if the APIs you need are available.

通常,我们建议您以最低版本的.NET Standard为目标。 这里的目标是重用。 您还可以检出可移植性分析器,并在现有库上运行它,以查看所需的API是否可用。

.NET Portability Analyzer

.NET Standard is what you target for your libraries, and the apps that USE your library target a platform.

.NET Standard是您针对库的目标,而使用您库的应用则针对一个平台。

Diagram showing .NET Framework, Core, and Mono sitting on top the base of .NET Standard

I emailed them back briefly, "Try making the library netstandard instead."

我给他们发了简短的电子邮件,“尝试改成网络标准库。”

They emailed back just a short email, "Yes! That did the trick!"

他们只回了一封简短的电子邮件,“是的!做到了!”

Sponsor: Big thanks to Raygun! Don't rely on your users to report the problems they experience. Automatically detect, diagnose and understand the root cause of errors, crashes and performance issues in your web and mobile apps. Learn more.

赞助商:非常感谢Raygun ! 不要依靠用户来报告他们遇到的问题。 自动检测,诊断和了解Web和移动应用程序中错误,崩溃和性能问题的根本原因。 了解更多

翻译自: https://www.hanselman.com/blog/how-to-reference-a-net-core-library-in-winforms-or-net-standard-explained

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值