c# loop through properties of a class code example
Example 1: loop over object properties c#
foreach (PropertyInfo prop in someObject.GetType().GetProperties())
{
Console.WriteLine($"{prop.Name}: {prop.GetValue(someObject, null)}");
}
Example 2: c# loop class properties add to array
Record record = new Record();
PropertyInfo[] properties = typeof(Record).GetProperties();
foreach (PropertyInfo property in properties)
{
property.SetValue(record, value);
}
Example 3: C# traverseall elements in class property
SomeObject.GetType().GetProperties().ToList().ForEach(x => Console.WriteLine($"{x.Name} = {x.GetValue(SomeObject, null)}"));