What I want to do, in very short version Summary:
var localType = typeof(MyDTOClass);
var result = MyGenericMethod<localType>(string path);
VisualStudio is telling me that dtoType is variable but is used like a type.
Long version: I have the following dictionary, which is used to read some files from disk then using a (de)serializer convert it into objects. Contents of json files are almost identical so I am only putting one as example: Json Contents,array of objects:
[{
"id": "1",
"areaType": "2",
"backgroundImagePath": "path",}, {
"id": "2",
"areaType": "2",
"backgroundImagePath": "path",}, {
"id": "99",
"areaType": "1",
"backgroundImagePath": "path",}]
Dictionary Contents:
var filesToParse = new Dictionary<string, Type>{
{ "area.json",typeof(AreaDto) },
{ "areaObject.json",typeof(AreaObjectDto) },
{ "monsterSymbol.json",typeof(MonsterSymbolDto) },
};
Code to retrieve contents of contents of json:
IEnumerable GetDeserializedJson(string pathToJsonFile, Type dtoType) {
IEnumerable result;
using (TextReader file = File.OpenText(pathToJsonFile)) {
result = JsonSerializer.DeserializeFromReader<IEnumerable<dtoType>>(file);
}
return result.OfType<dtoType>.Where(x => x != null);
}
My problem is with the following lines
result = JsonSerializer.DeserializeFromReader<dtoType>(file);
result.OfType<dtoType>.Where(x => x != null);
dtoType is variable but is used like a type. I know it is a variable but it is a variable of Type. Can you please explain me why i cant pass this type variable and if possible how to further move with my problem. The following is how I (want to) call my GetDeserializedJson
foreach(var keyValuePair in filesToParse){
GetDeserializedJson<keyValuePair.Value>(keyValuePair.Key);
}
Thanks in advance