【发布时间】:2011-09-03 01:11:12
【问题描述】:
是否可以(通过反射?)迭代对象的所有字段,并在每个字段上调用一个方法。
我有这样的课:
public class Overlay
{
public Control control1;
public Control control2;
}
我想要一个类似这样的方法:
public void DrawAll()
{
Controls[] controls = "All instances of Control"
foreach (Control control in Controls)
{
control.Draw()
}
}
这可以吗?我已经能够获取 Control 类的所有元数据,但这仅与类型有关,与特定实例无关。
我知道这看起来很奇怪,但我有我的理由。我使用的是 Unity 3D,每个控件实际上都是由编辑器实例化的 GUI 控件。
感谢您的帮助。
【问题讨论】:
-
对不起,我应该提到我希望 DrawAll() 方法与 Control 实例在同一个类中。
标签: c# reflection methods field