mercredi 14 septembre 2022

How can I search an IEnumerable list of objects for fields based on a text command?

I am trying to make a function that you can pass a IEnumerable of objects and a string with a dot notation command and have the function search through the objects within the list and pick out the values if they exist.

Such as Tool.Laser.x would search through the objects in the list and find The tool object with a field named Laser and return it's field named x's value. Also it should be so that it doesn't matter what structure object you pass it, it should be able to search through all of the objects and fields for the names you specify in the string passed in.

My issue is being able to traverse the fields inside each object in the list.. like to be able to iterate over them and evaluate them and find new fields to evaluate their values. I am really new to reflection, I can get the fields if I know the structure but I am unsure how to iterate through the fields and search for the name specified by the text command passed. I looked into LinQ but from what I have seen you have to know the names of the fields to do anything with it. Any help would be appreciated.





Aucun commentaire:

Enregistrer un commentaire