[英]In Visual Studio, is it possible to target a .NET 4.x CLR but a 2.0 class library API?
When you select the target framework of a C# project in Visual Studio, you actually select three different things at once (as far as I understand).当您在 Visual Studio 中 select 是 C# 项目的目标框架时,您实际上是同时 select 三个不同的东西(据我了解)。
Selecting .NET 4.7.2 will select:选择 .NET 4.7.2 将 select:
Now, is it possible to somehow configure these things independently?现在,是否有可能以某种方式独立配置这些东西? I understand there are dependencies, but targeting a lower BCL API should work, right?
我知道存在依赖关系,但是针对较低的 BCL API 应该可以工作,对吧?
The reason is this: my project is a C# class library that is used inside a Unity3D application.原因是这样的:我的项目是在 Unity3D 应用程序中使用的 C# class 库。 Unity has a 4.x (compatible) CLR, but let's you chose between a .NET Standard 2.0 API and a 4.x.
Unity 有一个 4.x(兼容)CLR,但让我们在 .NET 标准 2.0 API 和 4.x 之间进行选择。 It generally makes sense to target 2.0 because it is smaller and 4.x does not work on all platforms that Unity supports.
以 2.0 为目标通常是有意义的,因为它更小,而且 4.x 并不适用于 Unity 支持的所有平台。
It would be great if I could replicate this configuration in Visual Studio.如果我可以在 Visual Studio 中复制此配置,那就太好了。 Otherwise I might accidentally make an invalid (ie > 2.0) API call in my dll code and will only find out when the code crashes with a 'class/function not found' in Unity.
否则我可能会不小心在我的 dll 代码中调用无效(即 > 2.0)API 并且只会在代码崩溃并在 Unity 中出现“找不到类/函数”时才知道。
The language version can be set completely independently via <LangVersion>
in the csproj, ie语言版本可以通过csproj中的
<LangVersion>
完全独立设置,即
<LangVersion>8.0</LangVersion>
The CLR and BCL versions are... complex. CLR 和 BCL 版本是……复杂的。 You're really targeting a TFM here.
你真的在这里瞄准一个TFM。 It sounds like you want (again, in the csproj)
听起来你想要(再次,在 csproj 中)
<TargetFramework>netstandard2.0</TargetFramework>
The actual CLR (runtime) is determined by whatever runs it , not the library.实际的 CLR(运行时)由运行它的任何东西决定,而不是由库决定。 But you can multi-target, if that becomes useful, ie
但是你可以多目标,如果这变得有用的话,即
<TargetFrameworks>netstandard2.0;net472</TargetFrameworks>
and configure different dependencies and #if
etc for different platforms, to make the most of each.并为不同的平台配置不同的依赖项和
#if
等,以充分利用每个平台。 Whether unity can properly consume a multi-targeted package is another question, that I can't answer. unity是否可以正确使用多目标 package 是另一个问题,我无法回答。
You can edit the csproj at any point;您可以随时编辑 csproj; you aren't tied to what is generated from the template.
您不受模板生成的内容的约束。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.