Suppose I have a Class which consists of other classes which have properties like this:
public class classA {
public classAA classAA = new classAA();
public classAB classAB = new classAB();
}
public class classAA {
public string propertyA { get; set; }
}
public class classAB {
public string propertyB { get; set; }
}
And I want to iterate over all classA fields, then iterate over their properties and get their values. I have created simple console application to test this.
classA classA = new classA();
classA.classAA.propertyA = "A";
classA.classAB.propertyB = "B";
foreach (FieldInfo memberAField in classA.GetType().GetFields()) {
Console.WriteLine(memberAField.Name + " " + memberAField.MemberType + " " + memberAField.FieldType);
foreach (PropertyInfo classAProperty in memberAField.FieldType.GetProperties()) {
Console.WriteLine("name: " + classAProperty.Name);
Console.WriteLine(" to value: " + classAProperty.GetValue(memberAField));
}
}
Now I don't know what to pass to GetValue function. I have tried everything I could think of, but I always get error "Object does not match target type." I think the problem is that memberAField is FieldInfo object, but how can I get the actual System object?
EDIT: Thank you both for answers! Also, I do not think that this should be marked as a duplicate as I see this as different problem. Titles are similiar, yes.