0
string path;
string info;
Teacher teacher;

Let's say for example, I need to retrieve the name of the school that the teacher works for. The logic would be:

info = teacher.school.name;

Simple enough right? Is it possible obtain this information through a string path?

//************Pseudo
//assigning the path
path = "teacher.school.name"
//returning the information by passing the object and its path through the parameters
info = GetInfo(teacher, path);

Can this also be achieved with integers, boolean, and floats as well? Any assistance is greatly appreciated. Thanks!

Rishabh Agarwal
  • 1,988
  • 1
  • 16
  • 33
  • 2
    It is possible with Reflection but I question the merit of a design that requires this. Why do you have a string that represents the path to your data? – Crowcoder Jul 01 '20 at 19:56
  • With reflection it is possible. The last element in your path could be strings, integers, booleans or floats converted to string. And everything before needs to be objects. – casenonsensitive Jul 01 '20 at 19:59
  • @Crowcoder It's for game development. My object is modular. It has a value and it manipulates the value of other classes. All I would need to do is have a reference to the object and link the value of modular object to my manipulated object at the start of the game by using a string. Otherwise, I would have to hardcode it. – Bioject Jul 01 '20 at 20:29
  • You could split the string on `.` and [use Reflection to get property value by name](https://stackoverflow.com/questions/1196991/get-property-value-from-string-using-reflection). This would work similarly for fields if that is what you have. – Crowcoder Jul 01 '20 at 20:33

0 Answers0