如何迭代 C# 类查找特定类型的所有实例,然后在每个实例上调用方法

发布于 2024-11-10 03:53:47 字数 528 浏览 6 评论 0原文

是否可以(通过反射?)迭代对象的所有字段,并在每个字段上调用一个方法。

我有一个像这样的课程:

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 控件。

感谢您的任何帮助。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

太阳公公是暖光 2024-11-17 03:53:47
public class Program
{
    static void Main(string[] args)
    {
        Overlay overlay = new Overlay();
        foreach (FieldInfo field in overlay.GetType().GetFields())
        {
            if(typeof(Control).IsAssignableFrom(field.FieldType))
            {
                Control c = field.GetValue(overlay) as Control;
                if(c != null)
                    c.Draw();
            }
        }
    }
}

注意:这将过滤掉类中非控件的字段。此外,IsAssignableFrom 将为任何满足以下条件的字段类型返回 true:继承自 Control,假设您也希望处理这些字段。

public class Program
{
    static void Main(string[] args)
    {
        Overlay overlay = new Overlay();
        foreach (FieldInfo field in overlay.GetType().GetFields())
        {
            if(typeof(Control).IsAssignableFrom(field.FieldType))
            {
                Control c = field.GetValue(overlay) as Control;
                if(c != null)
                    c.Draw();
            }
        }
    }
}

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.

零度℉ 2024-11-17 03:53:47
var props = typeof(Overlay).GetProperties().OfType<Control>();
var props = typeof(Overlay).GetProperties().OfType<Control>();
挖鼻大婶 2024-11-17 03:53:47
Overlay obj = new Overlay();
Type t = typeof(Overlay);
FieldInfo[] fields = t.GetFields();
foreach (FieldInfo info in fields)
{
    Control c = (Control)info.GetValue(obj);
    c.Draw();
}

请注意,如果您的对象也没有 Control 字段,则需要通过 GetValue() 添加额外的返回值类型检查。

Overlay obj = new Overlay();
Type t = typeof(Overlay);
FieldInfo[] fields = t.GetFields();
foreach (FieldInfo info in fields)
{
    Control c = (Control)info.GetValue(obj);
    c.Draw();
}

Note, that you need to add additional check of type of returned value by GetValue() if your object has also not Control fields.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文