简体   繁体   English

C# 编译器抛出语言版本 (LangVersion) 引用错误“无效的‘可空’值:‘Enable’ for C# 7.3”

[英]C# compiler throws Language Version (LangVersion) reference error "Invalid 'nullable' value: 'Enable' for C# 7.3"

I have solution with couple .NET Standard projects in all I wanted to enable c# 8 and nullable like below:我有几个 .NET 标准项目的解决方案,我想启用 c# 8 并且可以为空,如下所示:

<PropertyGroup>
    <TargetFramework>netstandard2.1</TargetFramework>
    <LangVersion>8.0</LangVersion>
    <Nullable>enable</Nullable>
  </PropertyGroup>

Note: These settings are found in your.csproj file.注意:这些设置位于您的 .csproj 文件中。

The problem is that some projects are compiling fine and some have error:问题是有些项目编译正常,有些有错误:

Invalid 'nullable' value: 'Enable' for C# 7.3.无效的“可空”值:C# 的“启用” 7.3。 Please use language version 'preview' or greater请使用“预览”或更高版本的语言

I have Visual Studio 16.2 Preview 2 and .NET Core 3 Preview 6. Is this a bug in preview or I'm doing something wrong?我有 Visual Studio 16.2 Preview 2 和 .NET Core 3 Preview 6。这是预览中的错误还是我做错了什么?

In my case, I ran into this problem with Visual Studio 2022 when I changed the target framework from .NET Standard 2.1 to .NET Standard 2.0.就我而言,当我将目标框架从 .NET Standard 2.1 更改为 .NET Standard 2.0 时,Visual Studio 2022 遇到了这个问题。 I solved my problem by removing <Nullable>enable</Nullable> in the.csproj file and restarting Visual Studio.我通过删除 .csproj 文件中的<Nullable>enable</Nullable>重新启动 Visual Studio 解决了我的问题。

Original.csproj file:原始.csproj文件:

<PropertyGroup>
  <TargetFramework>netstandard2.1</TargetFramework>
  <Nullable>enable</Nullable>
</PropertyGroup>

New.csproj file:新建.csproj 文件:

<PropertyGroup>
  <TargetFramework>netstandard2.0</TargetFramework>
</PropertyGroup>

您应该尝试<LangVersion>preview</LangVersion>因为错误消息提示。

I had error like this "Invalid 'nullable' value: 'Enable' for C# 7.3. Please use language version '8.0' or greater" and I was able to solve it by changing order of specified targeted frameworks.我有这样的错误“无效的'nullable'值:C# 7.3的'Enable'。请使用语言版本'8.0'或更高版本”,我能够通过更改指定目标框架的顺序来解决它。

<TargetFrameworks>net6.0;net48</TargetFrameworks> <TargetFrameworks>net6.0;net48</TargetFrameworks>

To

<TargetFrameworks>net48;net6.0</TargetFrameworks> <TargetFrameworks>net48;net6.0</TargetFrameworks>

There is another solution... add some conditions to your project file to only use the new nullable feature for the target frameworks that support it.还有另一种解决方案...向您的项目文件添加一些条件,以便仅对支持它的目标框架使用新的可空特性。

There is an excellent article here ... and all credit to the author for the import code snippets below: 这里有一篇很棒的文章……下面的导入代码片段全部归功于作者:

<!-- Set the LangVersion = 8 -->
<PropertyGroup>
    <TargetFrameworks>netstandard2.0;netstandard2.1</TargetFrameworks>
    <LangVersion>8.0</LangVersion>
</PropertyGroup>

<!-- Only enable nullable feature for the supported frameworks -->
<PropertyGroup Condition=" '$(TargetFramework)' != 'netstandard2.0' ">
    <Nullable>enable</Nullable>
</PropertyGroup>

And then to hide warning messages in the unsupported frameworks:然后在不受支持的框架中隐藏警告消息:

<PropertyGroup Condition=" $(Nullable) != 'enable' ">
  <NoWarn>$(NoWarn);CS8632</NoWarn>
</PropertyGroup>

I had had the same issue when I have to downgrade compile-time supported version of C# language from 11.0 to 7.0.当我不得不将 C# 语言的编译时支持版本从 11.0 降级到 7.0 时,我遇到了同样的问题。 I resolved my issue for .net 7.0 project and was able to compile it within VS2022 with simple replacement from 'enable' to 'disable'.我解决了 .net 7.0 项目的问题,并且能够在 VS2022 中通过从“启用”到“禁用”的简单替换来编译它。

See the final result:查看最终结果:

<PropertyGroup>
   <TargetFramework>net7.0</TargetFramework>
   <ImplicitUsings>disable</ImplicitUsings>
   <Nullable>disable</Nullable>
   <LangVersion>7.0</LangVersion>
</PropertyGroup>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么添加<langversion>默认</langversion>到.csproj 文件修复C# 语言版本错误? - Why does adding <LangVersion>default</LangVersion> to a .csproj file fix a C# language version error? C#MonoDevelop:无效的-langversion CS1617 - C# MonoDevelop: Invalid -langversion CS1617 C# 语言版本为 7.3 但 IDE 仍然允许我添加? (空合并运算符) - C# langversion at 7.3 but IDE still allows me to add ?? (null-coalescing operator) 用于检测 C# 8/可空引用类型的编译器指令 - Compiler Directive for detecting C# 8/nullable reference types 哪个版本的C#编译器引入了可为空的类型? - Which version of the C# compiler introduced nullable types? 使用CSharpCodeProvider类编译C#7.3的哪个C#编译器版本? - Which C# compiler version to compile C# 7.3 with the CSharpCodeProvider class? 启用可空引用类型时 C# 匿名类型警告 - C# anonymous type warning when Nullable Reference Types is enable C#将输出参数更改为无效值并引发错误 - C# changes output parameter to invalid value and throws error C# 8 的可空引用类型 - Nullable Reference Types with C# 8 C#编译器版本和语言版本的区别 - Difference between C# compiler version and language version
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM