CallerArgumentExpression always null

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

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

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.



Read more here: https://stackoverflow.com/questions/64897434/callerargumentexpression-always-null

Content Attribution

This content was originally published by Paul Grenyer at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: