简体   繁体   中英

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. 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.

How do I go about finding out what specific type of Control I have in my list?

You could use the Object.GetType() method:

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:

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.

You can also use LINQ:

ControlsList.OfType<CheckBox>();
ControlsList.OfType<ICheckBoxControl>();

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

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:

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>:

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM