I understand how to use Reflection to get the property name and value of a given class. But what if that property is an array containing 1 or more of another class?
var person = new
{
Name = "John",
Characteristic = new[]
{
new Characteristic {Name = "Age", Value = "31"},
new Characteristic {Name = "Height", Value = "6'1""},
}
};
var properties = person.GetType().GetProperties();
foreach (var property in properties)
{
Console.WriteLine(property.Name);
Console.WriteLine(property.GetValue(person));
if (property.PropertyType.IsArray)
{
// Need to get info here! (See below)
}
}
Basically, I need to get that the particular property is an array of Characteristic
and then the properties Name
and Value
and then the subsequent values of those properties. Thanks very much!
**edit: this is what I tried and I couldn't get the values I needed...this code was in place of the comment above
foreach (var prop in property.GetType().GetProperties())
{
Console.WriteLine(prop.Name);
Console.WriteLine(prop.GetValue(property));
}