添加链接
link管理
链接快照平台
  • 输入网页链接,自动生成快照
  • 标签化管理网页链接
相关文章推荐
紧张的柠檬  ·  python做项目日程表-掘金·  1 年前    · 
闷骚的灌汤包  ·  curl转python工具 ...·  2 年前    · 
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

AllowNull Attribute in c#8 for netstandard2.0 and net451 raise compilation error [duplicate]

Ask Question

Environment: vs 2019 v16.3.1

in c# netstandard2.0 and net451 project, I use c# 8 nulable reference with attributes.

Enable c# 8 in csproj:

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

In myclass.cs, I use AllowNullAttribute:

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

When building the project, I get a compilation error:

CS0246 The type or namespace name 'AllowNullAttribute' could not be found (are you missing a using directive or an assembly reference?) ConsoleApp1 (netstandard2.0)

The documentation Attributes extend type annotations include these types

What is wrong in this code?

There is no reference in c# 8 docs to exclude these frameworks, see my reference in the question. – M.Hassan Nov 15, 2019 at 4:17

C# 8 (and therefore, by extension, AllowNull) does not seem to be fully supported in netstandard 2.0.

For example, if you go to the documentation for AllowNull and select .NET Standard 2.0 as the version, you will get the following message:

The requested page is not available for .NET Standard 2.0. You have been redirected to the newest product version this page is available

Also see this Github issue. Namely,

Officially, C# 8.0 is only supported on runtimes that adhere to the .NET Standard 2.1. That does not (and will not) include .NET Framework