【问题标题】:list all property names of controls based on condition in C#在 C# 中根据条件列出控件的所有属性名称
【发布时间】:2016-08-19 07:06:57
【问题描述】:

我正在尝试从某个页面 (this.Page.Controls) 解析我所有控件的所有属性(例如 ascx 文件),以获得具有我指定的属性值的属性的名称,例如 - 具有“这是我的标题”值的属性的名称是什么? (很可能是一个包含该值的文本框)。

以下返回此错误:

无法获取的内部内容,因为内容不是文字。 System.Web.UI.HtmlControls.HtmlContainerControl.get_InnerHtml()

不确定它的含义或如何纠正它。

 public String searchMethod(List<Control> listOfControls, String searchedValue)
        {
            String result = "";

            foreach (var control in listOfControls)
            {
                PropertyInfo[] properties = control.GetType().GetProperties();
                foreach (PropertyInfo property in properties)
                {
                    if (property.PropertyType == typeof (string)) //added condition on this line *******************************
                    {
                        if (property.GetValue(control, null) != null)
                            if (property.GetValue(control, null).ToString().Contains("searched String"))
                            {
                                result = result + property.Name + "/" + property.GetValue(control, null) + "/";
                            }
                    }

                }
            }
            return result;
        }

我想它卡在了一个不符合要求的属性上,那么为什么不直接移动到下一个直到找到合适的位置? 显然,卡住的属性是System.String Inner.Html

PS。我已经测试了提供给该方法的 listOfControls 并且它正在正确生成

后期更新: 我正在尝试的另一种方法是:

public string Method1(List<Control> controlList, string propName)
        {
            string result = "";
            foreach (var control in controlList)
            {
                foreach(var prop in control.GetType().GetProperties())
                {
                    if(prop.PropertyType == typeof(string))
                    {
                        if((prop.GetValue(control,null).GetType()) == typeof(string))
                            if (prop.GetValue(control, null).ToString().Contains(propName))
                        result += prop.Name + "######";
                    }
                }
            }
            return result;
        }

但是有了这个我得到对象引用未设置为对象的实例。在线if((prop.GetValue(control,null).GetType()) == typeof(string))

【问题讨论】:

    标签: c# user-controls controls propertyinfo


    【解决方案1】:

    这是记录在案的 HtmlContainerControl.InnerHtml 的行为 - 在“异常”部分中,记录了它会抛出 HttpException 如果:

    有不止一个 HTML 服务器控件。
    - 或 -
    HTML 服务器控件不是 System.Web.UI.LiteralControl 或 System.Web.UI.DataBoundLiteralControl。

    听起来你的情况正在发生后一种情况。代码不会“继续”,因为异常被抛出但未被捕获。

    老实说,我建议对您检查的控件类型更加挑剔,我可能会使用特定属性而不是仅仅调用ToString()

    【讨论】:

    • 谢谢乔恩·斯基特!我一直在尝试应用您的建议并提出了两个版本(至少),我重新编辑了我最初的问题。有什么想法吗?第二个版本看起来更简单、更整洁,但最终会引发错误。我的语法不灵巧。
    • @Halle:如果属性返回 null,那将会失败。但这是不必要的——如果属性类型是string,它不能返回任何other 而非string 或空引用。更重要的是,您仍然会询问导致最初问题的同一属性的值。
    • @Halle:如果该属性返回 null,那么您将在 null 引用上调用 GetType() - 砰。导致问题的属性 (HtmlContainer.InnerHtml) 不是“各种类型”——它是字符串类型。基本上,据我所知,您的更改并没有避免最初的问题。
    • 您能详细说明一下吗?我知道询问属性值类型是多余的,因为我已经知道属性类型。尚不清楚的是“您仍然会询问导致您最初问题的同一房产的价值。”我最初的问题是我正在分析的属性本身就是一个对象,而现在我仅限于字符串类型。但我仍然得到“对象引用未设置为对象的实例”。在我试图获取财产价值的那一行。明显地?该值为 null 所以不能是 .ToString()-ed 但如何解决呢?
    • @Halle:不,这不是问题所在。查看失败的属性的文档:它是一个字符串属性。
    【解决方案2】:

    如果控件内部有控件,您将收到该错误。您是否需要搜索可能在列表中的控件内部的控件?如果是这样,您需要使用递归并继续深入研究控件(以及其中的任何控件),并且仅在不包含其他控件的控件上检查 InnerHtml

    您也可以通过仅检查property.PropertyType == typeof(string) 时的值来简化这一点。您还可以将搜索范围限制在两个属性 - TextInnerHtml(如果适用)。

    【讨论】:

    • 非常感谢斯科特!我添加了这个条件,但得到“无法获取内部内容,因为内容不是文字”。关于如何改善这一点的任何想法? if (property.PropertyType == typeof (string)) - 你可以在编辑的问题中看到它
    猜你喜欢
    • 2019-07-27
    • 2017-10-23
    • 1970-01-01
    • 2019-09-30
    • 1970-01-01
    • 2014-12-22
    • 1970-01-01
    • 2021-10-07
    • 1970-01-01
    相关资源
    最近更新 更多