Looping through Object's properties in C Sharp
Loop to Find All Properties Info of an Object 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:
using System.Reflection;
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.
Have Fun,
Elad Shalom,
CTO at ITweetLive.com
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:
using System.Reflection;
Now, lets say I have an object called User and this object has 2 public properties:
- UserID.
- Name.
int testID = 123456;
User usr = new User(testID); // this is constructor to get user by ID
StringBuilder sb = new StringBuilder();
PropertyInfo[] properties = user.GetType().GetProperties();
foreach (PropertyInfo pi in properties)
{
sb.Append(
string.Format("Name: {0} | Value: {1}",
pi.Name,
pi.GetValue(user, null)
)
);
}
divResults.InnerText = sb.ToString();
This little piece of code will fill your HTML element with the properties names and values.
Have Fun,
Elad Shalom,
CTO at ITweetLive.com
nice approach,,,really good....
ReplyDeletecoach outlet store online clearance
ReplyDeletemichael kors outlet clearance
coach outlet store
ralph lauren outlet
polo ralph lauren outlet online
ray ban sunglasses
gucci outlet online
yeezy boost 350 white
coach factory outlet online
true religion outlet
cheap ray ban sunglasses
longchamp bag
reebok outlet store
cheap nfl jerseys
burberry outlet online
canada goose jackets
louis vuitton outlet
michael kors bags
canada goose uk
coach factory outlet online
nhl jerseys wholesale
longchamp handbags
pandora charms uk
adidas shoes
kate spade outlet
toms outlet
coach outlet store online
moncler outlet store
stan smith adidas
uggs canada
christian louboutin shoes
adidas nmd r1
nike blazer
louis vuitton outlet
2016023caiyan
michael kors outlet online
ReplyDeleteadidas stan smith shoes
nike zoom running shoe
nike air force
longchamp sale
kyrie 3
yeezy boost 350
nike air zoom
lacoste outlet
michael jordan shoes
cheap ray ban sunglasses
ReplyDeletecoach outlet store online clearances
prada sunglasses
birkenstock shoes
timberland uk
polo shirts
adidas trainers uk
oakley outlet
true religion outlet
hollister kids
2017.3.15chenlixiang
nike cortez
ReplyDeletecanada goose
nike air max 97
supreme
kobe byrant shoes
golden goose outlet
air max 97
louboutin outlet
vapormax
caterpillar boots