可空性分析无法警告来自NameValueCollection的空值为什么?

问题描述

请考虑在.net core中启用了可空性分析的.csproj项目:

<Project Sdk="Microsoft.NET.Sdk">

    <PropertyGroup>
        <TargetFramework>netcoreapp3.1</TargetFramework>
        <RootNamespace>(...)</RootNamespace>
        <IsPackable>false</IsPackable>
        <LangVersion>latest</LangVersion>
        <Nullable>enable</Nullable>
        <WarningsAsErrors>nullable</WarningsAsErrors>
    </PropertyGroup>
(...)

现在考虑以下代码

public class NullabilityTests
{
    [Fact]
    public void test()
    {
        var nameValueCollection = new NameValueCollection();
        string nullValue = nameValueCollection["I do not exist"];
        Foo(nullValue);
    }

    private void Foo(string shouldBeNotNull) => Assert.NotNull(shouldBeNotNull);
}

代码可以编译并在没有警告的情况下运行,但是测试在Assert.NotNull上失败。因此,可空性分析未能检测到空值,甚至没有警告可能空值。为什么?如果发生这种情况,我怎么知道何时信任C#8可空性分析?

解决方法

NameValueCollection类型只是尚未更新。来自Introducing Nullable Reference Types in C#

但是,如果他们在您之后添加注释,那么情况就更麻烦了。在这样做之前,您将“错误地”将其某些输入和输出解释为非null。您会收到不应该得到的警告,并会错过应该收到的警告。您可能必须使用!在某些地方,因为您确实了解得更多。

(重点是我的)

尤其是,该类型声明为返回类型string,当启用可空引用类型时,这意味着该引用不能为空。编译器没有理由考虑,所以不会发出警告。

由于您知道null是可能的返回值,因此由您决定执行该值。您可以通过简单地声明具有正确类型的接收变量来做到这一点:

string? nullValue = (string?)nameValueCollection["I do not exist"];

(您需要进行显式转换,因为否则编译器的静态分析仍然会将现在可以为空的string?变量视为非空。)

通常,解决此问题的理想方法是使用更现代的类型。例如,您可以将Dictionary<string,string?>用作等效的可感知空集合。但是,请注意,在这种情况下,您是从另一个.NET方法(HttpUtility.ParseQueryString())中获取集合。

如果您想要更强大的保护,则可以立即将集合复制到Dictionary<string,string?>,也可以为NameValueCollection类型实现包装器,以声明索引器使用更正确的{{1} }返回类型。

前者的一个可能例子是这样的:

string?
,

您创建了一个字符串集合-字符串自然是可为空的类型

您正在寻找一个不存在的项目,当您请求一个不存在的类型时,该类型将返回null

Caution

The Get method does not distinguish between null which is returned because the specified key is not found and null which is returned because the value associated with the key is null.

https://docs.microsoft.com/en-us/dotnet/api/system.collections.specialized.namevaluecollection?view=netcore-3.1