1

Given an infinitely nested object with unknown properties. What is the best approach to extract all the leaf value types (strings, ints, doubles and decimals).

Duplicate property names need to be retained.

Is there a good way to do this in C#.

Have tried expando and dynamic but it doesn't seem to work very well. The code quickly gets ridiculously complex. There must be a better way. I am thinking this must be a solved problem but I just can't seem to find a good example.

Andrew Gray
  • 3,593
  • 3
  • 35
  • 62
  • 2
    It sounds like you'll probably need to either convert it to representations of JSON objects (e.g. using JSON.NET with JToken or System.Text.Json with JsonElement) and then iterate through them, or you'll need to implement code using reflection to parse objects. Generics, and the various types of collections pose stumbling blocks when implementing such code. – ProgrammingLlama Aug 17 '22 at 02:31
  • The typical way would be with reflection. But what is the actual goal? What do you want to do with all the properties? – JonasH Aug 17 '22 at 07:05
  • Thank you for the tip @DiplomacyNotWar. This pointed me to a solution. – Andrew Gray Aug 17 '22 at 10:35
  • @JonasH That is beyond the scope of this question. – Andrew Gray Aug 17 '22 at 10:36
  • 1
    Generally questions such as Jonas' are asked in the hopes that we can provide a better solution to the overall problem, rather than the small part that's the subject of the question. – ProgrammingLlama Aug 17 '22 at 10:41

0 Answers0