Somewhere in my code I have an object that I already know that is a list. But I don't know the type parameter of that list. I need to iterate over it's items. I tried to cast that object to a list of objects but it didn't help me:
List<Object> objList = (List<Object>)(dataModel.Value);
foreach (var item in objList)
{
Console.WriteLine(item.ToString());
}
In the above code, the Value
property of dataModel
is a list of XYZ
values, but it throws an exception when I run this code. It says that, it could not cast XYZ
to Object
.
Is that possible to do some deserialization and do the job over deserialized objects?
IEnumerable<Object>
? – FealtyIList
. – Adrenalinforeach (object item in dataModel.Value)
shouldn't it? – PhrixusdataModel.Value
is a list. But I don't know type of items in that list. – ApplingdataModel.Value
is declared as a list. If it's declared asobject
your suggestion wouldn't work. – FealtydataModel.Value
is obviously not declared as a list - if it were the OP wouldn't have needed to cast it in the first place. – Fealtydatamodel.Value
from another assembly using reflection. I extractFieldInfo
of all items in runtime, but in compile time I do not have access to the type parameters, because I have reflected thedataModel
from an external assembly – Appling