Loop through an object's properties in C#
To loop through we useGetType()
function which returns an object of type Type
, this object has a function called GetProperties()
which returns a list of properties with Name and value.Type type = obj.GetType();
PropertyInfo[] properties = type.GetProperties();
foreach (PropertyInfo property in properties)
{
Console.WriteLine("Name: " + property.Name + ", Value: " + property.GetValue(obj, null));
}