我在一些C#源代码中发现以下行:
if(!(context.Compilation.GetTypeByMetadataName("Xunit.FactAttribute")
is { } factAttribute))
这里是另一个:
if(!(diagnostic.Location.SourceTree is { } tree))
is
运算符后面的花括号({ }
)是什么意思?
这是C#8.0中引入的一个新的模式匹配特性,称为property pattern。在此特定情况下,它用于检查对象是否不为空,例如链接文章:
static string Display(object o) => o switch
{
Point { X: 0, Y: 0 } p => "origin",
Point { X: var x, Y: var y } p => $"({x}, {y})",
{} => o.ToString(),
null => "null"
};
上一篇:在factorybot中附加ActiveStorage文件文件、factorybot、ActiveStorage
下一篇:如何用RSpec和FactoryGirl在Rails模型中测试BEFORE_UPDATE回调?回调、如何用、模型、测试