1

如果之前有人问过并回答过这个问题,我深表歉意,但我无法找到答案。

我知道如何浏览控件集合并获取所有控件的列表,包括子控件。

    void printControlTree(Control ctl, int indent)
    {
      string pad = "";
      for(int i=0; i < indent; i++)
      {
        pad += "   ";
      }

      Print(pad + "=> " + ctl.Name);

      if (ctl.Controls != null && ctl.Controls.Count > 0)
      {
        foreach (Control c in ctl.Controls)
        {
            printControlTree(c, indent+1);
        }
      }
     }

我想做的是获得每个控件和子控件中所有方法的列表。这可能吗?我会这样认为,因为有 Control.ControlCollection.Find 方法可以按名称在控件中查找特定方法,但我想要列出所有方法而不提前知道它们的名称。此外,是否可以获得控件中所有内容的列表:方法、输入字段等?您的帮助将不胜感激。谢谢你。

4

3 回答 3

5
static void PrintMethods(Object o) {

    Type t = o.GetType();
    MethodInfo[] methods = t.GetMethods();
    foreach(MethodInfo method in methods) {

        Print( method.Name );
    }


}
于 2013-01-22T21:09:18.053 回答
0

首先,我们将从一个返回控件的所有子控件的方法开始(递归地),而不仅仅是打印它们。

public static IEnumerable<Control> GetAllControls(Control control)
{
    Stack<Control> stack = new Stack<Control>();
    stack.Push(control);

    while (stack.Any())
    {
        var next = stack.Pop();
        yield return next;
        foreach (var child in next.Controls.OfType<Control>())
        {
            stack.Push(child);
        }
    }
}

接下来,我们将编写一个方法,给定一系列对象,将其转换为所有这些对象的所有方法的序列:

public static IEnumerable<MethodInfo> GetMethods<T>(IEnumerable<T> sequence)
{
    return sequence.GroupBy(obj => obj.GetType())
        .SelectMany(group => group.Key.GetMethods());
}

现在我们只调用它们:

foreach(var methodInfo in GetMethods(GetAllControls(someControl))
{
    //print method name
}

如果您认为您会使用这些方法足够多,您可能希望将它们转换为扩展方法。

于 2013-01-22T21:23:26.743 回答
-1

您可以在此处找到列出类成员的示例:

查看类型信息

using System;
using System.IO;
using System.Reflection;

class Mymemberinfo
{
    public static void Main()
    {
        Console.WriteLine ("\nReflection.MemberInfo");
        // Gets the Type and MemberInfo.
        Type MyType = Type.GetType("System.IO.File");
        MemberInfo[] Mymemberinfoarray = MyType.GetMembers();
        // Gets and displays the DeclaringType method.
        Console.WriteLine("\nThere are {0} members in {1}.",
            Mymemberinfoarray.Length, MyType.FullName);
        Console.WriteLine("{0}.", MyType.FullName);
        if (MyType.IsPublic)
        {
            Console.WriteLine("{0} is public.", MyType.FullName);
        }
    }
}

如果你已经有一个对象(比如一个控件),你可以像这样绕过上面的一些代码:

using System;
using System.IO;
using System.Reflection;

class Mymemberinfo
{
    public static void Main()
    {
        // Whatever kind of control you are using:
        Object l_control = new Object();

        Console.WriteLine ("\nReflection.MemberInfo");
        // Gets the Type and MemberInfo.

        // ----- Call l_control.GetType();
        Type MyType = l_control.GetType();
        MemberInfo[] Mymemberinfoarray = MyType.GetMembers();
        // Gets and displays the DeclaringType method.
        Console.WriteLine("\nThere are {0} members in {1}.",
            Mymemberinfoarray.Length, MyType.FullName);
        Console.WriteLine("{0}.", MyType.FullName);
        if (MyType.IsPublic)
        {
            Console.WriteLine("{0} is public.", MyType.FullName);
        }
    }
}

您可以在 MSDN 网站上阅读有关反射的更多信息:

.NET Framework 中的反射

MSDN 上的文章包括如何使用每个功能的完整描述和示例。

于 2013-01-22T21:07:05.940 回答