Ho implementato un metodo restituisce un List<string>
in base a una stringa json.deserialize stringa vuota a un elenco <string>
Sta funzionando bene ho capito che sto provando a deserializzare una stringa vuota. Non si ferma né solleva un'eccezione. Restituisce un valore null
invece uno vuoto List<string>
.
La domanda è: cosa posso toccare per restituire un valore vuoto List<string>
invece di null
?
return JsonConvert.DeserializeObject(content, typeof(List<string>));
EDIT metodo generico:
public object Deserialize(string content, Type type) {
if (type.GetType() == typeof(Object))
return (Object)content;
if (type.Equals(typeof(String)))
return content;
try
{
return JsonConvert.DeserializeObject(content, type);
}
catch (IOException e) {
throw new ApiException(HttpStatusCode.InternalServerError, e.Message);
}
}
'type.GetType()' è errato; darà del tipo concreto che eredita da 'System.Type' che non è quello che vuoi. Vuoi 'se (digita == typeof (Object))' lì. Nel prossimo 'se' puoi usare anche' == '(per coerenza). –