I have 3 class like below.
[Serializable]
public class Classa {
public list<Object> list {get; set;}
}
[Serializable]
public class Classb {
public string name {get; set;}
public string nickname {get; set;}
}
[Serializable]
public class Classc {
public string type {get; set;}
public string price {get; set;}
}
In my program, I want to cast List<Object>
to List<Classb>
or List<Classc>
when I pass the JSON structure matching with Classb
or Classc
... is there any way to cast it?
[HttpPost]
[ActionName("Import")]
[EnableCors(origins: "*", headers: "*", methods: "*")]
public Object Import([FromBody]Classa ImportData)
{
List<Classb> blist = a.list.Cast<Classb>().toList();
}
It throws:
Unable to cast object of type 'Newtonsoft.Json.Linq.JObject' to type 'Classb'
I have tried :
List<Classb> blist = aOfType<Classb>().ToList();
Success but no value..