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 Web サイトを参照してください。

.NET Framework でのリフレクション

MSDN の記事には、各機能の完全な説明と使用方法の例が含まれています。

于 2013-01-22T21:07:05.940 に答える