CallerArgumentExpression 始终为空
CallerArgumentExpression always null
我正在 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);
}
但是,我无法将 conditionExpression
的值设置为 null 以外的值。
我一直在使用这个 https://blog.mcilreavy.com/articles/2018-08/caller-argument-expression-attribute 和其他几个页面,当然,作为指南很好,但我无法使用它。
此页面 https://www.c-sharpcorner.com/article/c-sharp-8-features/ 声称它从未进入 C#8,现在是 C# Next。
该功能是在支持 C# 10 的 Roslyn 编译器中实现的,即使您没有使用 C# 10。
我正在 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);
}
但是,我无法将 conditionExpression
的值设置为 null 以外的值。
我一直在使用这个 https://blog.mcilreavy.com/articles/2018-08/caller-argument-expression-attribute 和其他几个页面,当然,作为指南很好,但我无法使用它。
此页面 https://www.c-sharpcorner.com/article/c-sharp-8-features/ 声称它从未进入 C#8,现在是 C# Next。
该功能是在支持 C# 10 的 Roslyn 编译器中实现的,即使您没有使用 C# 10。