Q:

How to get all the properties of an object including the properties of every child object?

I am trying to get all the properties from an object including all the properties of it's child objects, for example:
You have a class A:
public class A
{
    public Guid Id { get; set; };
    public string Name { get; set; };
    public B BObject { get; set; };
}

And then the B class could look like this:
public class B
{
    public Guid Id { get; set; };
    public string BValue { get; set; };
}

How can I make sure when I use PropertyInfo[] properties = a.GetType().GetProperties(); that I get all the properties for class A but also for class B toghether in the list?

A:

You can combine reflection with recursion to meet your needs：
 List<string> properties = new List<string>();
 GetAllProperties<A>(a,properties);

The method:
 public void GetAllProperties<T>(T type, List<string> properties)
    {
        try
        {
            // Get the properties of 'Type' class object.
            PropertyInfo[] myPropertyInfo;
            myPropertyInfo = type.GetType().GetProperties();
            for (int i = 0; i < myPropertyInfo.Length; i++)
            {
                if (!myPropertyInfo[i].PropertyType.Namespace.Contains("System"))
                {
                    object newtype = Activator.CreateInstance(myPropertyInfo[i].PropertyType);
                    GetAllProperties<object>(newtype, properties);
                }
                else
                {
                    properties.Add(myPropertyInfo[i].Name);
                }

            }

        }
        catch(Exception ex)
        {
            throw ex;
        }
    }

