Looping through Object's properties in C Sharp
Loop to Find All Properties Info of an Object In C#.
Have you ever tried to make a dynamic test page? To build and HTML element contains all the properties of a specific .Net object?
Well, I encountered this problem couple of days ago and while searching Google I barely found good matches. So I decided that for my sake, and yours, I will simply post a short explanation about how to do it, followed by a quick and simple example of how to loop though object properties in C#.
First I'll provide a short explanation about Reflection:
Reflection enables you to discover an object's properties, methods, events and properties values at run-time. Reflection can give you a full mapping of an object (well, it's public data).
In order to use reflection, and to loop through the object's properties, we'll first have to import the namespace:
Now, lets say I have an object called User and this object has 2 public properties:
This little piece of code will fill your HTML element with the properties names and values.
CTO at ITweetLive.com