Is there a way in c# to loop over the properties of a class?
Basically I have a class that contains a large number of property\'s (it basically holds the results of
I tried various recommendations on this page, I couldn't quite get any of them to work. I started with the top answer (you'll notice my variables are similarly named), and just worked through it on my own. This is what I got to work - hopefully it can help someone else.
var prop = emp.GetType().GetProperties(); //emp is my class
foreach (var props in prop)
{
var variable = props.GetMethod;
empHolder.Add(variable.Invoke(emp, null).ToString()); //empHolder = ArrayList
}
***I should mention this will only work if you're using get;set; (public) properties.