是否可以将所有这些合并为一个 foreach?

Is it possible to combine all of these into one foreach?

到目前为止,我已经尝试过 var 但无法正常工作。 代码:

var a = new { drives1, ipAdd1, spotify1, timeAndDate1, activeWindow1, reminders11, weather1 };

然后 foreach (Control/var c in a) 并得到错误,而且我确信必须有一个没有下面所有代码的简单方法。


当前代码:

        foreach (Control c in drives1.Controls)
        {
            if (c.Name.StartsWith("SC")) { c.ForeColor = Color.FromArgb(rr, gg, bb); }
        }
        foreach (Control c in ipAdd1.Controls)
        {
            if (c.Name.StartsWith("SC")) { c.ForeColor = Color.FromArgb(rr, gg, bb); }
        }
        foreach (Control c in spotify1.Controls)
        {
            if (c.Name.StartsWith("SC")) { c.ForeColor = Color.FromArgb(rr, gg, bb); }
        }
        foreach (Control c in timeAndDate1.Controls)
        {
            if (c.Name.StartsWith("SC")) { c.ForeColor = Color.FromArgb(rr, gg, bb); }
        }
        foreach (Control c in activeWindow1.Controls)
        {
            if (c.Name.StartsWith("SC")) { c.ForeColor = Color.FromArgb(rr, gg, bb); }
        }
        foreach (Control c in reminders11.Controls)
        {
            if (c.Name.StartsWith("SC")) { c.ForeColor = Color.FromArgb(rr, gg, bb); }
        }
        foreach (Control c in weather1.Controls)
        {
            if (c.Name.StartsWith("SC")) { c.ForeColor = Color.FromArgb(rr, gg, bb); }
        }

至少你可以让它与 2 个 foreach 一起工作:

var a = new List<ContainerControl> { drives1, ipAdd1, spotify1, timeAndDate1, activeWindow1, reminders11, weather1 };
foreach (var container in a) {
    foreach (Control c in container.Controls)
    {
        if (c.Name.StartsWith("SC")) { c.ForeColor = Color.FromArgb(rr, gg, bb); }
    }
}

如果 a 中的所有项目都有一个定义 Controls 的公共超级 class,那么您可以聚合所有控件,过滤您需要的控件,然后迭代它:

var allControls = a.SelectMany(i=>i.Controls.Where(c=>c.Name.StartsWith("SC"));

foreach (var control in allControls)
{
    c.ForeColor = Color.FromArgb(rr, gg, bb);
}

如果没有,那么你可以使用union

var allControls = drives1.Controls
    .Union(ipAdd1.Controls)
    .Union(spotify1.Controls)
    .Union ...
    .Where(c=>c.Name.StartsWith("SC");

添加方法:

private void SetColor( List<Control> control)
{
    foreach (Control c in control)
    {
       if (c.Name.StartsWith("SC")) { c.ForeColor = Color.FromArgb(rr, gg, bb); }
     }
}

迭代一个:

foreach (Control c in a)
{
   SetColor(c);
}

你可以这样做

private static void Edit()
{

   setForeColor(drives1.Controls);
   setForeColor(ipAdd1.Controls);
   setForeColor(spotify1.Controls);
   setForeColor(timeAndDate1.Controls);
   setForeColor(activeWindow1.Controls);
   setForeColor(reminders11.Controls);
   setForeColor(weather1.Controls);

}


private static void setForeColor(Controls controls)
{
        foreach (Control c in controls)
        {
            if (c.Name.StartsWith("SC"))
            { 
                c.ForeColor = Color.FromArgb(rr, gg, bb); 
            }
        }

}

您可以将它们加入一个 IEnumerable,然后使用 where 条件获取匹配器:

namespace ConsoleApp1 {

    public class Control {
        public Control(string name) {
            this.Name = name;
        }

        public string Name { get; set; }
    }

    class Program {
        static void Main(string[] args) {
            var list1 = new List<Control> { new Control("a"), new Control("b"), new Control("c") };
            var list2 = new List<Control> { new Control("a"), new Control("b"), new Control("c") };
            var list3 = new List<Control> { new Control("a"), new Control("b"), new Control("c") };

            foreach (var ctrl in GetList(list1, list2, list3).Where(c => c.Name.StartsWith("a"))) {
                ctrl.ForeColor = Color.FromArgb(rr, gg, bb);
            }
        }

        private static IEnumerable<Control> GetList (params IEnumerable<Control>[] controls) {
            return controls.SelectMany(x => x);
        }
    }
}

They're all UserControl's,

然后创建一个 List<UserControl> 作为集合。这样你就指定了公共数据类型,编译器就不会报错了。然后它将遍历它:

List<UserControl> a = new List<UserControl>{ drives1, ipAdd1, spotify1, timeAndDate1, activeWindow1, reminders11, weather1 };

foreach (UserControl uc in a)
{
    foreach (Control c in uc.Controls)
    {
        if (c.Name.StartsWith("SC")) { c.ForeColor = Color.FromArgb(rr, gg, bb); }
    }
}

解释:

如果您查看 documentation

中 class UserControl 的继承层次结构

您会发现它上面的 2 层继承自您在循环中使用的 Control class which has the property Controls。所以 UserControl 是您可以用于您的集合的最低共同祖先。