I wrote a simple API call within a method expecting a JSON response... Nothing spectacular:
// [API call...]
dynamic responseString = JsonConvert.DeserializeObject(response.Result.Content.ReadAsStringAsync().Result);
item.category = responseString.category;
item.shortDescription = responseString.shortDescription;
item.countryOfManufacture = responseString.manufacturerCountry ?? "DE";
Sometimes there are not all required parameters from the API available... So I tried to check for is null
and is string.Empty
and raise a dialog where the user can enter the missing values...
But this:
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;
}
or
if (string.IsNullOrEmpty(responseString.weight))
throws a FormatException.
If I check either is null
or is string.Empty
it works like a charm. I know the differnence between ref and value types and thought maybe there is a Problem... However, I like to know why it behaves like this...
Thanks in advance... and sorry for my english...
Markus
Ok, I get it... The dynamic
descriptor is the reason. You have to cast it to a string like this:
if (string.IsNullOrEmpty((string)responseString.weight))
...thanks for your effort
Markus