0

How do I get a list of all the properties of a class?

public class ReqPerson
{
    public String Name { get; set; }
    public String Age { get; set; }
    public List<Detail> Details { get; set; }
}

public class Detail
{
    public String Job { get; set; }
    public String City { get; set; }
}

This is my code, and the result only get properties class ReqPerson, not for class Detail.

 private static PropertyInfo[] GetProperties(object obj)
    {
        return obj.GetType().GetProperties();
    }

       ReqPerson req = new ReqPerson();
        // Get property array
        var properties = GetProperties(req);

        foreach (var p in properties)
        {
            string name = p.Name;
            var value = p.GetValue(Inq.ReqInquiry(req, null);
            Response.Write(name);
            Response.Write("</br>");
        }

Anybody can improve my code?

Roberts
  • 53
  • 1
  • 7
  • Please show the code you're using to call `GetProperties()`. – Matthew Watson Nov 04 '19 at 09:05
  • 1
    you need to check for a collection type when you arrive at a certain property and if it is a collection you need to iterate over it and get those properties of the elements of this collection – Mong Zhu Nov 04 '19 at 09:05
  • @MatthewWatson `var properties = GetProperties(object);` – Mong Zhu Nov 04 '19 at 09:06
  • That couldn't be your *exact* code as `object` is a keyword. I understand that's probably *similar* to your some code that demonstrates the problem, but often the important aspects are in the differences between what you show and what you're running. Please provide a [mcve], and it'll be a lot easier to help you. – Jon Skeet Nov 04 '19 at 09:08
  • ReqPerson req = new ReqPerson(); var properties = GetProperties(req); foreach (var p in properties) { string name = p.Name; var value = p.GetValue(req, null); Response.Write(name); Response.Write(""); } – Roberts Nov 04 '19 at 09:09
  • what about this line: `var value = p.GetValue(req, null)` and `var value = p.GetValue(Inq.ReqInquiry(req, null);` which one is the correct line? – Mong Zhu Nov 04 '19 at 09:15
  • Can you tell us what you want the output to look like? – Robin Bennett Nov 04 '19 at 09:16
  • I would almost say that [this is a fitting duplicate candidate](https://stackoverflow.com/a/1043778/5174469) – Mong Zhu Nov 04 '19 at 09:17
  • do you want only the names of alle properties of `Details` ? or do you want also the individual values of each element in you list? – Mong Zhu Nov 04 '19 at 09:18
  • I think the OP wants a recursive solution that descends through all the elements of a collection property. – Matthew Watson Nov 04 '19 at 09:18
  • 1
    Maybe take a step back and explain what you are trying to do here. For example, what would you expect to happen if a class had a child reference to itself? What would you expect the output to be then? This seems like an XY problem to me. – DavidG Nov 04 '19 at 09:19
  • @MatthewWatson "I think the OP wants a recursive solution that descends through all the elements of a collection property" in this case [this duplicate might fit](https://stackoverflow.com/a/20554262/5174469) – Mong Zhu Nov 04 '19 at 09:21
  • I want the output All properties in ReqPerson class and Details class. Because the result from my code is properties in ReqPerson only. – Roberts Nov 04 '19 at 09:22
  • you can see this link [enter link description here](https://stackoverflow.com/questions/737151/how-to-get-the-list-of-properties-of-a-class) – Mohammad alshikh khalil Nov 04 '19 at 09:46
  • "output All properties in ReqPerson class and Details class" that would mean basically you want a list with `{ Name, Age, Job, City }` did I understand you correctly? you are not looking for the values of each individual entry in the `List
    ` ? is that true?
    – Mong Zhu Nov 04 '19 at 09:56
  • @MongZhu ya thats rigth. Because My ouput only {Name, Age, Details}. – Roberts Nov 05 '19 at 03:19

2 Answers2

0
 public virtual ICollection<Detail> Details { get; set; }
zied ben othman
  • 82
  • 2
  • 14
0

You could use reflection to iterate over the type of Collection. For example

private IEnumerable<PropertyInfo> GetProperties(Type type)
{
    PropertyInfo[] properties = type.GetProperties();
    foreach (PropertyInfo property in properties)
    {
        if ( property.PropertyType.GetInterfaces()
               .Any(x => x == typeof(IList)))
        {
             foreach(var prop in GetProperties(property.PropertyType.GetGenericArguments()[0]))
                yield return prop;
        }
        else
        {
            if (property.PropertyType.Assembly == type.Assembly)
            {
                if (property.PropertyType.IsClass)
                {
                    yield return property;
                }
                GetProperties(property.PropertyType);
            }
            else
            {
                yield return property;
            }
        }
    }
}
Anu Viswan
  • 17,797
  • 2
  • 22
  • 51