C# 检查引用是否为 null - 运行时绑定错误

C# check if a reference is null - runtime binding error

我收到此错误:

Cannot perform runtime binding on a null reference

当我尝试将 JSON 对象分配给 var

var vrlvl = dataFirst.payload.last_quick_matches.csgo.skill_level;

如何在分配对象之前检查对象是否为空?

我试过这些,但它给出了同样的错误

if (dataFirst.payload.last_quick_matches.csgo.skill_level != null)

if (Object.ReferenceEquals(null, dataFirst.payload.last_quick_matches.csgo.skill_level)) 

您可能还需要检查几层:

if (dataFirst && dataFirst.payload && dataFirst.payload.last_quick_matches && dataFirst.payload.last_quick_matches.csgo && dataFirst.payload.last_quick_matches.csgo.skill_level)
{
    var vrlvl = dataFirst.payload.last_quick_matches.csgo.skill_level;
}

编辑:试试这个:

if (dataFirst != null && dataFirst.payload != null && dataFirst.payload.last_quick_matches != null && dataFirst.payload.last_quick_matches.csgo != null && dataFirst.payload.last_quick_matches.csgo.skill_level != null)
{
    var vrlvl = dataFirst.payload.last_quick_matches.csgo.skill_level;
}