0

I would like to search a class (and it's properties which are also instances of classes) for a specific property(name). In the current code i'm only getting it's own property.

example classes:

    public class Root
{
    public List<CodeTypes> Codes { get; set; }
    public Descendant Child { get; set; }
}

public class Descendant
{
    public List<CodeTypes> Codes { get; set; }
    public AnotherDescendant SomeChild { get; set; }
}

public class AnotherDescendant
{
    public List<CodeTypes> Codes { get; set; }
}

public enum CodeTypes
{
    Ok,
    NotOk,
    DontNow
}

In my current solution i'm only getting the list from the Root class.

 var MyCodes = itemToClean.GetType().GetProperties().Where(p => p.Name.Equals("Codes", StringComparison.OrdinalIgnoreCase));

I'm looking for a GetProperties which returns all the List lists from the descendants of a class. Can this be done with reflection (without recurrance)?

update: This question was closed, but i was hoping to get an answer which would not was centered arount recursiveness. In my current solution recursiveness is the only fix. I was trying to work around recursiveness.

Obelix
  • 708
  • 11
  • 24
  • By "descendant", do you mean *type inheritance*, or do you mean the runtime object graph? if the latter: you'd need to recursively walk the object model to discover them; in the same way that to do this in C# you'd need to do `obj.Foo.Bar`, etc – Marc Gravell Jul 05 '22 at 10:31
  • This question was closed, but i was hoping to get an answer which would not was centered arount recursiveness. In my current solution recursiveness is the only fix. I was trying to work around recursivness. – Obelix Jul 06 '22 at 11:10
  • but this is inherently a recursive problem :shrug: – Marc Gravell Jul 06 '22 at 14:30

0 Answers0