c#8中的netstandard2.0和net451的AllowNull属性会引起编译错误

问题描述 投票:0回答:1

环境:vs 2019 v16.3.1

在c#netstandard2.0和net451项目中,我使用带有属性的c#8可空引用。

在csproj中启用c#8:

    <LangVersion>8.0</LangVersion>
    <Nullable>enable</Nullable>

在myclass.cs中,我使用AllowNullAttribute:

     class My class {
       [AllowNull]
     public T MyValue {get;set;}
     }

构建项目时,出现编译错误:

CS0246找不到类型或名称空间名称'AllowNullAttribute'(您是否缺少using指令或程序集引用?)ConsoleApp1(netstandard2.0)

文档Attributes extend type annotations包括这些类型

此代码有什么问题?

c# roslyn-code-analysis c#-8.0 nullablereferencetypes
1个回答
0
投票

C#8(因此,扩展名为AllowNull)在netstandard 2.0中似乎不受完全支持。

例如,如果转到AllowNull的documentation并选择.NET Standard 2.0作为版本,则会收到以下消息:

请求的页面不适用于.NET Standard 2.0。你有已被重定向到最新产品版本为。

另请参阅this Github issue。即,>

正式地,仅在符合以下要求的运行时中才支持C#8.0:.NET Standard 2.1。那不(也不会)包括.NET Framework4.x。

© www.soinside.com 2019 - 2024. All rights reserved.