How do I write something like this?
string json = null;
Type[] types = Assembly.GetExecutingAssembly().GetTypes();
var carTypes = (from type in types
where type.IsClass && type == typeof(Car)
select type);
foreach(var carType in carTypes )
{
string typeName = carType .Name;
string jsonFile = typeName + ".json";
json = File.ReadAllText(jsonFile);
// How can I not hardcode "Porche" here and instead use "carType"?
IList<Porche> cars = Newtonsoft.Json.JsonConvert.DeserializeObject<List<Porche>>(json);
}
Here, Porche is a sub class of Car - Both "Porche" and "Car" are Class. In additional to Porche, we have Toyata, BMW, Tesla... I want to dynamically retrieve them via Reflection (avoid hardcoding) "cars" is a list of instances of cars.
"carType" is Type. How can I use "carType" in JsonConvert.DerserializeObject which take List as argument?
IList<someReflectionAPI(carType)> cars = Newtonsoft.Json.JsonConvert.DeserializeObject<List<someReflectionAPI(carType)>>(json);
Thanks
Aucun commentaire:
Enregistrer un commentaire