Home
Manage Your Code
Snippet: Using Reflection to loop thru properties of a given object (C#)
Title: Using Reflection to loop thru properties of a given object Language: C#
Description: Using Reflection to loop thru properties of a given object Views: 254
Author: sl prince Date Added: 2/27/2008
Copy Code  
1 // create some entity

2  SomeEntity se = new SomeEntity ();
3  Type objectType = se.GetType();
4  PropertyInfo[] properties =  objectType.GetProperties();
5 
6   //Reflection rawks!!!!!

7   foreach (PropertyInfo property in properties)
8   {
9      //you can then set the value for the given property here

10      property.SetValue(se, someValue, null);
11   )
Usage
// create some entity
  SomeEntity se = new SomeEntity ();
  Type objectType = se.GetType();
  PropertyInfo[] properties =  objectType.GetProperties();
 
   //Reflection rawks!!!!!
   foreach (PropertyInfo property in properties)
   {
      //you can then set the value for the given property here
      property.SetValue(se, someValue, null);
   )