C# 查找控件中具有属性的属性

C# 查找控件中具有属性的属性,c#,reflection,attributes,custom-attributes,C#,Reflection,Attributes,Custom Attributes,我有一个包含许多控件的页面 当页面呈现时,我希望遍历页面上的所有控件,并找到具有特定属性的任何控件。我正试图用c#来实现这一点-有什么想法可以实现吗?您需要使用反射 使用反射可以获得对象的所有属性页面上的每个控件都有一个“controls”属性,该属性包含其所有子控件。我以前写过递归函数来循环这些函数,但手头上没有。让我试着快速写一篇: public Collection<Control> findControlsWithAttributes(Control startingCon

我有一个包含许多控件的页面


当页面呈现时,我希望遍历页面上的所有控件,并找到具有特定属性的任何控件。我正试图用c#来实现这一点-有什么想法可以实现吗?

您需要使用反射


使用反射可以获得对象的所有属性

页面上的每个控件都有一个“controls”属性,该属性包含其所有子控件。我以前写过递归函数来循环这些函数,但手头上没有。让我试着快速写一篇:

public Collection<Control> findControlsWithAttributes(Control startingControl)
{
    Collection<Control> toReturn = new Collection<Control>();
    foreach (Control curControl in startingControl.controls)
    {
        if (DO COMPARISON HERE WITH CURCONTROL) toReturn.add(curControl);
        if (curControl.Count() > 0) findControlsWithAttributes(curControl, toReturn);
    }
    return toReturn;
}

private void findControlsWithAttributes(Control startingControl, Collection<Control> inputCollection)
{
    foreach (Control curControl in startingControl.controls)
    {
        if (DO COMPARISON HERE WITH CURCONTROL) inputCollection.add(curControl);
        if (curControl.Count() > 0) findControlsWithAttributes(Control startingControl, Collection<Control> inputCollection);
    }
}
公共集合找到属性为的控件(控件启动控件)
{
Collection toReturn=新集合();
foreach(启动控制中的控制电流控制。控制)
{
if(此处与CURCONTROL进行比较)toReturn.add(CURCONTROL);
如果(curControl.Count()>0)找到属性为(curControl,toReturn)的控件;
}
回归回归;
}
私有void find控件属性(控件启动控件、集合输入集合)
{
foreach(启动控制中的控制电流控制。控制)
{
if(此处与CURCONTROL进行比较)inputCollection.add(CURCONTROL);
如果(curControl.Count()>0)找到属性为的控件(控件启动控件、集合输入集合);
}
}
我这么做已经有一段时间了,我记不清Collection.Count是否是一个方法或属性,因此请确保先检查它,但如果传入该页面,则这将检查页面上的每个服务器可见控件,并返回一个包含与比较匹配的控件的集合


最后,Control.Attributes将返回一个AttributeCollection,您应该能够随后对其进行比较。

不确定您所追求的是什么属性,但如果类属性是您所追求的属性,那么从另一方面看@user751975,您可以做如下操作

page.Controls.Cast<System.Web.UI.WebControls.WebControl>().First().Attributes["class"]
page.Controls.Cast().First().Attributes[“class”]

我不知道你的控制树有多大。这就是我要做的。我不是在承诺最好的表演

案例1。查找.NET属性

IEnumerable<Control> GetMarkedControls(ControlCollection controls)
{
  foreach(Control c in controls)
  {
    var props = c.GetType().Properties();
    if(props.Count(x => x.GetCustomAttributes(false).OfType<YourAttribute>().Count() > 0) > 0)
      yield return c;

    foreach (Control ic in GetMarkedControls(c.Controls))
      yield return ic;
  }
}
IEnumerable GetMarkedControls(ControlCollection控件)
{
foreach(控件中的控件c)
{
var props=c.GetType().Properties();
if(props.Count(x=>x.GetCustomAttributes(false).OfType().Count()>0)>0)
收益率c;
foreach(GetMarkedControls中的控制ic(c.Controls))
收益率集成电路;
}
}
案例2。查找HTML属性

IEnumerable<WebControl> GetMarkedControls(ControlCollection controls)
{
  foreach(Control c in controls)
  {
    if(c is WebControl)
    {
      var wc = c as WebControl;
      if (wc.Attributes.FirstOrDeafult(x => x.Name == "yourAttribute") != null)
        yield return c;
    }

    foreach (Control ic in GetMarkedControls(c.Controls))
      yield return ic;
  }
}
IEnumerable GetMarkedControls(ControlCollection控件)
{
foreach(控件中的控件c)
{
如果(c是网络控制)
{
var wc=c作为网络控制;
if(wc.Attributes.FirstOrDeafult(x=>x.Name==“yourAttribute”)!=null)
收益率c;
}
foreach(GetMarkedControls中的控制ic(c.Controls))
收益率集成电路;
}
}
现在您可以这样调用它:
var controlsWAttribute=GetMarkedControls(this.Controls)从您的页面或任何控件。这样,您就不会被迫在页面级别调用它

使用此方法,可以递归地浏览页面或控件中的整个控件树