【发布时间】:2016-12-12 06:41:10
【问题描述】:
这是我的代码
private void FindControls<T>(Control control, List<T> list)
{
foreach (Control c in control.Controls)
{
if (c != null)
{
if (c is T)
{
list.Add(c); //<-- Problem is here
}
else
{
FindControls<T>(c, list);
}
}
}
}
我收到了消息
"最好的重载匹配 'System.Collection.Generic.List.Add(T)' 有一些无效参数"
这是另一种查找特定控件的方法
private void MyMethod(Employee e)
{
List<MyUserControlType> employees = new List<MyUserControlType>();
this.FindControls<MyUserControlType>(this.MyControlRoot, employees);
foreach (var employee in employees)
{
....
}
}
我想返回不继承自 Control 类型的 MyUserControlType 类型的控件列表。从 UserControl 继承
我该如何解决这个问题?
【问题讨论】:
-
你必须将 C 转换为 T。
标签: c# list generics recursion