如何使用 JetBrains 注释指定 Func 对象不返回 null?

Jos*_*ech 5 c# resharper visual-studio

我有一些代码:

public void Foo([NotNull] Func<string> bar)
{
    //whatever
}
Run Code Online (Sandbox Code Playgroud)

我的问题是, [NotNull] 注释指定 bar 本身不为空。我正在寻找一个注释,该注释表明 BAR 返回的内容不为空。

我预计这是可能的,但似乎不可能。他们支持 IEnumerables 的 ItemNotNull,所以我不明白为什么我们不能为 Func 对象提供 ReturnsNotNull。

我在这里错过了一些明显的东西吗?当然,这个基本的东西是受支持的。

Jos*_*ech 0

现在,随着Nullable 引用类型的引入,这已成为 C# 8.0 中的一个内容。

我们现在可以这样做:

Func<string> //describes a not-null func that returns a not-null string
Func?<string> //describes a nullable func that returns a not-null string
Func<string?> //describes a not-null func that returns a nullable string
Func?<string?> //describes a nullable func that returns a nullable  string
Run Code Online (Sandbox Code Playgroud)