mardi 5 juillet 2022

C# get all underlaying properties of with a specific name via reflection [duplicate]

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)?





Aucun commentaire:

Enregistrer un commentaire