Is it possible (via reflection?) to iterate all fields of an object calling a method on each one.
I have a class like so:
public class Overlay
{
public Control control1;
public Control control2;
}
I’d like a method that goes something like this:
public void DrawAll()
{
Controls[] controls = "All instances of Control"
foreach (Control control in Controls)
{
control.Draw()
}
}
Can this be done? I’ve been able to get all the metadata on the Control class but this relates only to the type not the specific instance.
I know this seems odd but I have my reasons. I’m using Unity 3D and each control is actually a GUI control instantiated by the editor.
Thanks for any help.
Note: This will filter out fields in the class that aren’t controls. Also, IsAssignableFrom will return true for any field types that inherit from Control, assuming you wish to handle these fields as well.