First of all, avoid using reflection unless you really need it. He is slow, he is dirty, he is borderline undebuggable (and I love him, but it's different)
If you want to dump all the content of your object, I would recommend passing it to the objects themselves, they should know their internal state. You can use the ToString method to record your internal state, or you can define an interface to display internal state
interface IStateDisplay { string GetInnerState(); }
and your objects implement it. Then the code to display the properties will be
Console.WriteLine(user.GetInnerState());
Another option would be to use a tool like AutoMapper , which hides the complexities and subtleties of reflection from you and provides a good API to use.
However, if you are studying reflection, printing the state of a complex object is a good exercise. A few pointers in this direction:
public string Name;
is not a property, this is a field, so type.GetProperties() will not return it. Read what C # properties are and how they are used and defined. Declaring a Minimum Property
public string Name {get; set;}
In addition, prp.GetType() will return type information for the PropertyInfo type, and not for the type of the property it contains. In this case, you will need the prp.PropertyType property.
Further, I do not think Type.IsPrimitive is what you want. This property returns true for Boolean, Byte, SByte, Int16, UInt16, Int32, UInt32, Int64, UInt64, IntPtr, UIntPtr, Char, Double and Single and false for everything else. Most importantly, typeof(string).IsPrimitive returns false.
In the same note, I do not think that checking Type.IsClass is what you want. Since you use it, it only checks if the property has a value or a reference type, and also how the value types ( struct ) can also be very complex and contain their own properties and fields, the check does not make sense.