c# - Iterate object list and get element property with a string -
i looking solution solve problem have.
i got object:
- person (string id, string firstname, string lastname) i got list couple persons
- list<person> persons; what goal is:
1. iterate through list 2. property value element string named property for example:
i want know id of person in list:
i can this:
foreach(person p in persons) { string personid = p.id; } but want this:
foreach(person p in persons) { string personid = p. + "id"; } i want append string "id" id of element, instead of calling p.id.
is possible? need solution this, , have spent lot of time couldn't find solution on internet.
you use reflection value of property name:
foreach (person p in persons) { var personid = typeof(person).getproperty("id").getvalue(p, null) string; } of course needs error handling , null checks, gist :)
Comments
Post a Comment