如何查找实现接口并调用方法的所有对象 [英] How to find all objects that implement a interface and invoke a method

查看:132
本文介绍了如何查找实现接口并调用方法的所有对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个UserControl,有几个子UserControl,而那些UserControl都有子UserControl.

I have a UserControl that have a few child UserControl's and those UserControl's have child UserControl's.

考虑一下:

MainUserControl
  TabControl
    TabItem
      UserControl
        UserControl
          UserControl : ISomeInterface
    TabItem
      UserControl
        UserControl
          UserControl : ISomeInterface
    TabItem
      UserControl
        UserControl
          UserControl : ISomeInterface
    TabItem
      UserControl
        UserControl
          UserControl : ISomeInterface

这是我到目前为止所拥有的,但是没有找到ISomeInterface:

This is what i have so far, but finds no ISomeInterface:

PropertyInfo[] properties = MainUserControl.GetType().GetProperties();
foreach (PropertyInfo property in properties)
{
    if (typeof(ISomeInterface).IsAssignableFrom(property.PropertyType))
    {
        property.GetType().InvokeMember("SomeMethod", BindingFlags.InvokeMethod, null, null, null);
    }
}

是否有可能这样找到通过反射实现ISomeInterfaceMainUserControl的所有子UserControl,并在该接口上调用方法(void SomeMethod())?

Is it possible so find all the child UserControl's from MainUserControl that implement ISomeInterface via reflection and call a method(void SomeMethod()) on that interface?

推荐答案

您将需要递归地遍历MainUserControl中的所有子控件.

You will need to recursively iterate through all the subcontrols within your MainUserControl.

以下是您可以使用的帮助方法:

Here's a helper method you can use:

/// <summary>
/// Recursively lists all controls under a specified parent control.
/// Child controls are listed before their parents.
/// </summary>
/// <param name="parent">The control for which all child controls are returned</param>
/// <returns>Returns a sequence of all controls on the control or any of its children.</returns>

public static IEnumerable<Control> AllControls(Control parent)
{
    if (parent == null)
    {
        throw new ArgumentNullException("parent");
    }

    foreach (Control control in parent.Controls)
    {
        foreach (Control child in AllControls(control))
        {
            yield return child;
        }

        yield return control;
    }
}

然后:

foreach (var control in AllControls(MainUserControl))
{
    PropertyInfo[] properties = control.GetType().GetProperties();
    ... Your loop iterating over properties

或者(如果这样对您有用,那就更好了,因为它要简单得多):

Or (much better if this will work for you, since it's a lot simpler):

foreach (var control in AllControls(MainUserControl))
{
    var someInterface = control as ISomeInterface;

    if (someInterface != null)
    {
        someInterface.SomeMethod();
    }
}

或者,使用Linq(需要using System.Linq来做到这一点):

Or, using Linq (need a using System.Linq to do this):

foreach (var control in AllControls(MainUserControl).OfType<ISomeInterface>())
    control.SomeMethod();

这似乎是最好的. :)

Which seems best of all. :)

这篇关于如何查找实现接口并调用方法的所有对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆