我有一個在List<Control>
中保存的控件列表,我希望能夠檢查它們的類型。由於我的列表只包含控件,所以做typeof()不會讓我太過分,我想要問能否List<Control>[0]
是複選框,文本框,標籤等。從列表中獲取特定類型的對象<T>
我該如何去尋找在我的列表中列出哪些具體的控制類型?
我有一個在List<Control>
中保存的控件列表,我希望能夠檢查它們的類型。由於我的列表只包含控件,所以做typeof()不會讓我太過分,我想要問能否List<Control>[0]
是複選框,文本框,標籤等。從列表中獲取特定類型的對象<T>
我該如何去尋找在我的列表中列出哪些具體的控制類型?
你可以使用Object.GetType()方法:
var controls = new List<Control>();
// Add Controls
if(controls[0].GetType() == typeof(Checkbox))
{
// I'm a checkbox
}
else if (controls[0].GetType() == typeof(TextBox))
{
// I'm a TextBox
}
...等等。
或者,如果你不介意的話,你也可以匹配你檢查控件的孩子,你可以使用is
操作:
var controls = new List<Control>();
// Add Controls
if(controls[0] is Checkbox)
// I'm a Checkbox or a child of Checkbox
else if (controls[0] is TextBox)
// I'm a TextBox or a child of TextBox
您想使用GetType()來獲取動態類型。
您可以循環:
foreach (var ctl in ControlsList)
{
if (ctl is CheckBox)
//Do this
else if (ctl is TextBox)
//DoThis
}
有了這個,如果使用ITextControl,ICheckBoxControl,IButtonControl,您可以獲得更好的靈活性,因爲這將多個控件組合在一起成爲一個條件。
你也可以使用LINQ:
ControlsList.OfType<CheckBox>();
ControlsList.OfType<ICheckBoxControl>();
HTH。
Control control = list[0]
bool isCheckBox = control is CheckBox;
bool isTextBox = control is TextBox;
bool isLabel = control is Label;
稍有不同的實現使用LINQ獲得一個列表控件每種類型
var checkBoxes = from l in controls
where l.GetType() is CheckBox
select l;
var textBoxes = from l in controls
where l.GetType() is TextBox
select l;
var labels = from l in controls
where l.GetType() is Label
select l;
'controls.OfType
您可以用gettype正是如此:
using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ConsoleApplication1
{
class Program
{
static IList<Control> controls;
static void Main(string[] args)
{
controls = new List<Control>();
controls.Add(new TextBox());
controls.Add(new CheckBox());
foreach (var control in controls)
{
Console.WriteLine(control.GetType());
}
Console.ReadLine();
}
}
}
或者,你可以設置一個字典,如果這就是更多你的風格:
using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ConsoleApplication1
{
class Program
{
static IDictionary<string, Control> controls;
static void Main(string[] args)
{
controls = new Dictionary<string, Control>();
controls.Add("textbox thing", new TextBox());
controls.Add("checkbox thing", new CheckBox());
foreach (var control in controls)
{
Console.WriteLine(control.Key);
}
Console.ReadLine();
}
}
}
或者,可以添加控制作爲一類的屬性,並與IList設置爲IList的< ClassA的>:
using System;
using System.Collections.Generic;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace ConsoleApplication1
{
class ClassA
{
public Control Control { get; set; }
public string Group { get; set; }
public string Subgroup { get; set; }
}
class Program
{
static IList<ClassA> controls;
static void Main(string[] args)
{
controls = new List<ClassA>();
controls.Add(new ClassA
{
Control = new TextBox(),
Group = "Input",
Subgroup = "Text"
});
controls.Add(new ClassA
{
Control = new CheckBox(),
Group = "Input",
Subgroup = "Checkbox"
});
foreach (var control in controls)
{
Console.WriteLine(control.Subgroup);
}
Console.ReadLine();
}
}
}
VAR複選框= Controls.Where(三 - > c是複選框)
+ 1 for .OfType() – spender 2010-11-09 19:52:46