I have a method that receives a list of objects:
private void escribe(List<object> lista)
that a priori I don't know what class they are and I want to know at run time the properties of each of the objects in the list.
I have tried with:
Type type = lista.GetType();
var PropertyInfos = lista.GetType().GetProperties();
I have also tried with:
Type tip = lista.GetType();
if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List<>))
{
var itemType = type.GetGenericArguments()[0];
}
But I can't retrieve the properties of the object in question. A greeting and thanks for the help.
Edit: I add information of the class with which I am tested, although I really want it to work for everyone.
public class Prueba
{
string prop1;
int prop2;
bool prop3;
float prop4;
//List<string> prop5;
object prop6;}
And all these fields are transformed to properties with the first letter in uppercase
The problem you have is that you are accessing the properties of
List<object>
and from what I want to imagine is that you need to access the properties of the object itself.If so, you have to go through them and get the type of the specific object and not the one from the list
The problem you have is that you are getting the type of the
List
(which will always be of typeList
, when what you want is to get the type of theobject
within that list, to obtain its properties.Let's imagine this example. We have a test class:
Now we define your list
List<object>
and add an object of typeEjemplo
:Notice that I use
First
to access the first element of the list, and from it I get the type first and then its properties.