Running .NET Standard binaries on different frameworks

六眼飞鱼酱① 提交于 2019-12-18 08:45:29

问题


I understand targeting .NET Standard 2 means that the frameworks between .net core, mono and the full .net framework, but I'd like to understand what that means for the binaries created by the different frameworks.

So if I create a console program targeting .NET Standard 2 and compile using .NET Framework, can only the full .NET Framework run this file? If they can all run the files, how would I run the .NET Core .dll executable console using the full framework or Mono?

So basically are the binaries created by each framework (targeting .NET Standard) able to run using the other frameworks?


回答1:


I believe the Comparison to Portable Class Libraries section probably says it best:

.NET Standard is the replacement for Portable Class Libraries (PCL).

.NET Standard is not itself a runtime it is a type forwarding mechanism for multiple different runtimes. Therefore, it is not possible to create anything but a non-executable class library in .NET Standard, just as was the case with PCLs.

This enables the class library to be consumed by executable assemblies that target specific runtimes (.NET Framework, .NET Core, Xamarin.iOS, Mono, etc).

It is helpful to think of this in terms of classes and interfaces. In pseudo-code, .NET Standard is an interface that .NET Framework and .NET Core implement.

public interface INetStandard
{
    // Only has API definitions
}

public class NetFramework : INetStandard
{
    // .NET Framework Runtime implemented here
}

public class NetCore : INetStandard
{
    // .NET Core Runtime implemented here
}

This makes it possible to use .NET Standard with either .NET Framework or .NET Core, but .NET Standard itself has no runtime, only a set of APIs that can be shared between runtimes. You can target any one of the three with your project, but you can't execute .NET Standard any more than you can instantiate an interface.

Unfortunately, you are not the first to have asked about this and unless Microsoft makes the documentation more clear that .NET Standard does not actually execute, you likely won't be the last.



来源:https://stackoverflow.com/questions/48822040/running-net-standard-binaries-on-different-frameworks

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!