Tuesday, 3 September 2013

Iterate properties and nested first level properties

Iterate properties and nested first level properties

I have the following classes
public class Car
{
public int CarId { get; set;}
public string Description { get; set;}
public Engine Engine { get; set;}
}
public class Engine
{
public int EngineId { get; set;}
public int Description { get; set;}
}
Now i want to iterate all the properties in Car and all the properties in
Engine, i dont want to hardcode the property names "Car or Engine"
Example to get all properties of Car, where obj is instance of Car.
var properties = obj.GetType().GetProperties(BindingFlags.FlattenHierarchy
| BindingFlags.Instance | BindingFlags.Public);
But this doesnt iterate the properties of Engine.

No comments:

Post a Comment