my requirement is to dynamically map the json to C# model using newtonsoft.json or any other.
Let say i have these classes in my C# project.
ModelA {string FirstName}
ModelB{int DepartmentId}
so if i get json as {"FirstName":"John"}
, it should resolve to ModelA
object. likewise for json {"DepartmentId":8}
, it should resolve to ModelB
var model = JsonConvert.DeserializeObject<**What should come here as i want this dynamic**>(**Json for ModelA or ModelB**);
How to acheive this?
Editing question. Thanks Guru for solutuon.
the other part of requirement is the model i will resolve from json, i need to pass the type of resolved Model to the generic method as below (i.e. in place of T where T is class). How to do this.
_unitOfWork.Repository <T where T is class>.Add(modelA)
doing something like Type type = typeof(modelA)
and then doing unitOfWork.Repository <type>.Add(modelA)
gives error type is variable but is used like type Please help. looking this for long time. could not find satisfying solution anywhere.