vendredi 8 octobre 2021

Determine type of Object in Controller Class

I have a controller mapped to API endpoint which takes a list of objects (say Dog) and then I deserialize it into IEnumerable.How do I know if this object is of type Dog or Cat or whatever ? My controller looks like this

string objectType  = "Dog"  //can be anything like Dog,Cat etc

var list = System.Text.Json.JsonSerializer
    .Deserialize<IEnumerable<objectType>>(content.ToString());

var output = services.InsertObject<objectType>(list);

Obviously I cannot write IEnumerable<objectType> since it throws error. Since I want to pass on this dynamic Object type to other layers as well, I want it to be generic. My methods look like this. InsertObject<T>(IEnumerable<T>items);





Aucun commentaire:

Enregistrer un commentaire