CallerArgumentExpression 始终为空 [英] CallerArgumentExpression always null

查看:50
本文介绍了CallerArgumentExpression 始终为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I'm experimenting with [CallerArgumentExpression] in C# 8:

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);
}

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

However, I cannot get the value of conditionExpression to be anything other than null.

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

I've been using this https://blog.mcilreavy.com/articles/2018-08/caller-argument-expression-attribute and a few other pages and, of course, good as a guide, but I can't get it to work.

推荐答案

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

This page https://www.c-sharpcorner.com/article/c-sharp-8-features/ claims that it never made it to C#8 and is now C# Next.

另见https://github.com/dotnet/csharplang/issues/287 尤其是底部.

这篇关于CallerArgumentExpression 始终为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆