我在一个期望 JSON 响应的方法中编写了一个简单的 API 调用......没什么了不起的:
// [API call...]
dynamic responseString = JsonConvert.DeserializeObject(response.Result.Content.ReadAsStringAsync().Result);
item.category = responseString.category;
item.shortDescription = responseString.shortDescription;
item.countryOfManufacture = responseString.manufacturerCountry ?? "DE";
有时 API 中并非所有必需的参数都可用......所以我尝试检查is null
并is string.Empty
引发一个对话框,用户可以在其中输入缺失值......
但是这个:
if (responseString.weight == null ||responseString.weight == string.Empty )
{
DialogArgs args = new DialogArgs();
args.Add("Gewicht");
OnMissingValue?.Invoke(args);
item.weight = args.Get<float>("Gewicht");
}
else
{
item.weight = Convert.ToSingle(responseString.weight) / 1000;
}
或者
if (string.IsNullOrEmpty(responseString.weight))
抛出FormatException。如果我检查is null
或者is string.Empty
它就像一个魅力。我知道 ref 和 value 类型之间的区别,并认为可能存在问题......但是,我想知道它为什么会这样......
在此先感谢...对不起我的英语...
马库斯