简体   繁体   English

在 Visual Studio 中,是否可以定位 .NET 4.x CLR 但 2.0 class 库 API?

[英]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:

  • the language version (eg C# 7.2)语言版本(例如 C# 7.2)
  • the CRL (common language runtime) version (eg 4.5) CRL(公共语言运行时)版本(例如 4.5)
  • and the BCL (base class library) version (eg 4.7.2)和 BCL(基础 class 库)版本(例如 4.7.2)

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.

相关问题 目标.NET 4.x和.NET Standard 2.0 - Target .NET 4.x and .NET Standard 2.0 无法在Visual Studio 2015中定位.Net 2.0 - Cannot target .Net 2.0 in Visual Studio 2015 .NET Core 2.0 不是 Visual Studio Target Framework 中的一个选项 - .NET Core 2.0 Is Not An Option in Visual Studio Target Framework 在Core 2.0 Web API项目中引用了.net Framework 4.x程序集的app.config迁移? - app.config migration for .net framework 4.x assemblies referenced in Core 2.0 Web API project? 将 class 库项目升级到 .net 标准 2.1,其中 .net 框架 4.X 项目已引用该标准 - Upgrading class library project to .net standard 2.1 where this has been referenced by .net framework 4.X projects 无法在Visual Studio 2012中针对.NET 3.5的C#项目中使用CLI类库中的CLR类型,并使用Windows7.1SDK工具集进行编译 - Unable to use a CLR type from CLI class library in a C# project in Visual Studio 2012 targeting .NET 3.5 and compiling with Windows7.1SDK toolset 包括在使用Unity时在Travis中以.NET 4.x为目标的依赖项 - Including Dependencies That Target .NET 4.x in Travis While Using Unity Visual Studio - 创建以.Net Core为目标的类库 - Visual Studio - Create Class library targeting .Net Core 使用Visual Studio 2012分析.NET类库时,没有收集任何数据 - No data collected when profiling .NET class library with Visual Studio 2012 Visual Studio 2019 中的 .Net Standard 1.0 class 库 - .Net Standard 1.0 class library in Visual Studio 2019
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM