CallerArgumentExpression 始终为 null

Pau*_*yer 5 c# .net-core c#-8.0

我正在 C# 8 中试验 [CallerArgumentExpression]:

static void Main(string[] args)
{
    try
    {
        Program query = null;
        Argument(query != null, "Ooops");
    }
    catch (Exception e)
    {
        Console.WriteLine(e.Message);
    }
}

public static void Argument(bool condition, string message, [CallerArgumentExpression("condition")] string conditionExpression = null)
{
    if (!condition) throw new ArgumentException(message: message, paramName: conditionExpression);
}
Run Code Online (Sandbox Code Playgroud)

但是,我无法将 的值conditionExpression设为 null 以外的任何值。

我一直在使用这个https://blog.mcilreavy.com/articles/2018-08/caller-argument-expression-attribute和其他一些页面,当然,作为指南很好,但我无法得到让它工作。

Ric*_*lls 7

此页面https://www.c-sharpcorner.com/article/c-sharp-8-features/声称它从未进入 C#8,现在是 C# Next。

另请参阅https://github.com/dotnet/csharplang/issues/287,尤其是底部。

  • 看起来它即将在 C# 10 中实现。(大约 11 月,所以几周后) (3认同)