[英]Get specific type of object from List<T>
I have a list of Controls that are held in a List<Control>
and I want to be able to check their type. 我有一个List<Control>
中保存的控件List<Control>
,我希望能够检查它们的类型。 As my list holds only Controls, doing typeof() isn't going to get me too far, I want to be able to ask if List<Control>[0]
is a Checkbox, TextBox, Label, etc. 因为我的列表只包含控件,所以执行typeof()不会让我太过分,我想能够询问List<Control>[0]
是Checkbox,TextBox,Label等。
How do I go about finding out what specific type of Control I have in my list? 我如何找到列表中具有哪种特定类型的控件?
You could use the Object.GetType() method: 您可以使用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
}
...and so on. ...等等。
Or, if you don't mind that you might also match children of the controls you're checking for, you can use the is
operator: 或者,如果您不介意也可能匹配您正在检查的控件的子项,则可以使用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
You can loop: 你可以循环:
foreach (var ctl in ControlsList)
{
if (ctl is CheckBox)
//Do this
else if (ctl is TextBox)
//DoThis
}
With this, you get better flexibility if you use the ITextControl, ICheckBoxControl, IButtonControl, as this groups together multiple controls into one condition. 有了这个,如果你使用ITextControl,ICheckBoxControl,IButtonControl,你会获得更好的灵活性,因为它将多个控件组合成一个条件。
You can also use LINQ: 你也可以使用LINQ:
ControlsList.OfType<CheckBox>();
ControlsList.OfType<ICheckBoxControl>();
HTH. HTH。
您想使用GetType()来获取动态类型。
Control control = list[0]
bool isCheckBox = control is CheckBox;
bool isTextBox = control is TextBox;
bool isLabel = control is Label;
Slightly different implementation with Linq to get a list of controls for each type 与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;
You can use GetType thusly: 您可以这样使用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();
}
}
}
Or, you can setup a Dictionary if that's more your style: 或者,您可以设置一个字典,如果这更符合您的风格:
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();
}
}
}
Or, you could add the Control as a property in Class A, and set the IList to IList<ClassA>: 或者,您可以将Control作为A类中的属性添加,并将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 checkboxes = Controls.Where(c - > c是CheckBox)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.