C# 列出控件中的所有方法
我很抱歉,如果这已经被问到和回答之前,但我无法找到一个答案 我知道如何遍历控件集合并获得所有控件(包括子控件)的列表C# 列出控件中的所有方法,c#,reflection,user-controls,C#,Reflection,User Controls,我很抱歉,如果这已经被问到和回答之前,但我无法找到一个答案 我知道如何遍历控件集合并获得所有控件(包括子控件)的列表 void printControlTree(Control ctl, int indent) { string pad = ""; for(int i=0; i < indent; i++) { pad += " "; } Print(pad + "=> " + ctl.
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);
}
}
}
void printControlTree(控制ctl、int缩进)
{
字符串pad=“”;
对于(int i=0;i”+ctl.Name);
如果(ctl.Controls!=null&&ctl.Controls.Count>0)
{
foreach(控制中的控制c)
{
printControlTree(c,缩进+1);
}
}
}
我想做的是也得到每个控件和子控件中所有方法的列表。这可能吗?我也这么认为,因为有Control.ControlCollection.Find方法可以按名称在控件中查找特定方法,但我想要一个所有方法的列表,而不需要提前知道它们的名称。此外,是否可以获得控件中所有内容的列表:方法、输入字段等。?非常感谢你的帮助。谢谢。您可以在此处找到列出类成员的示例: 如果您已经有了一个对象(例如控件),您可以像这样绕过上面的一些代码:
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网站上阅读有关反射的更多信息:
MSDN上的文章包括如何使用每个功能的完整描述和示例。您可以在此处找到列出类成员的示例:
static void PrintMethods(Object o) {
Type t = o.GetType();
MethodInfo[] methods = t.GetMethods();
foreach(MethodInfo method in methods) {
Print( method.Name );
}
}
如果您已经有了一个对象(例如控件),您可以像这样绕过上面的一些代码:
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网站上阅读有关反射的更多信息:
MSDN上的文章包括如何使用每个功能的完整描述和示例。首先,我们将从一个方法开始,该方法返回控件的所有子控件(递归),而不仅仅是打印它们
static void PrintMethods(Object o) {
Type t = o.GetType();
MethodInfo[] methods = t.GetMethods();
foreach(MethodInfo method in methods) {
Print( method.Name );
}
}
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<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);
}
}
}
如果你认为你会充分使用这些方法,你可能会想把它们转换成扩展方法。你说的“方法”是什么意思?看看反射。我建议你也学习如何格式化代码,使其可读。这是一个很好的建议。是的..反射会进行这些类型的操作…但这个问题涉及的范围太广了。这是不可回答的。你所说的“方法”是什么意思?看看反射。看,我建议你也学习如何格式化你的代码,使其可读,这只是一个好的建议。嗯。是的..反射会进行这些类型的操作…但这个问题涉及的范围太广了。这是不负责任的。这会让成员们感到不安。谢谢,这会让会员们高兴的。谢谢。尝试了此操作,但出现了一个错误:非泛型类型“System.Collections.IEnumerable”不能与类型参数一起使用CS0308通过使用Collections.generic修复了它,但出现了另一系列错误。@PBrenek您需要使用System.Collection.generic的
@PBrenek您还需要使用System.Linq
,和System.Reflection
。错误:参数“1”:无法从“System.Collections.Generic.IEnumerable”转换为“System.Collections.Generic.IEnumerable”CS1503@PBrenek这只适用于.NET4.0,其中添加了通用协方差。对于3.5,您需要将.Cast()
添加到GetAllControls
的结果中,或使GetAllControls
generic。尝试了此操作,但出现错误:非泛型类型“System.Collections.IEnumerable”不能与类型参数一起使用CS0308通过使用Collections.generic修复了它,但出现了另一系列错误。@PBrenek您需要使用System.Collection.generic
@PBrenek您还需要使用System.Linq
和System.Reflection
。错误:参数“1”:无法从“System.Collections.Generic.IEnumerable”转换为“System.Collections.Generic.IEnumerable”CS1503@PBrenek这只适用于.NET4.0,其中添加了通用协方差。对于3.5,您需要将.Cast()
添加到GetAllControls
的结果中,或者将GetAllControls
设置为通用。