NumericUpDown类型的问题

时间:2010-07-19 13:14:30

标签: c# .net winforms controls numericupdown

当我做这样的事情时:

public static void BindData<T>(this System.Windows.Forms.Control.ControlCollection controls, T bind)
    {
        foreach (Control control in controls)
        {
            if (control.GetType() == typeof(System.Windows.Forms.TextBox) || control.GetType().IsSubclassOf(typeof(System.Windows.Forms.TextBox)))
            {
                UtilityBindData(control, bind);
            }
            else
            {
                if (control.Controls.Count == 0)
                {
                    UtilityBindData(control, bind);
                }
                else
                {
                    control.Controls.BindData(bind);
                }
            }
        }
    }

    private static void UtilityBindData<T>(Control control, T bind)
    {
        Type type = control.GetType();

        PropertyInfo propertyInfo = type.GetProperty("BindingProperty");
        if (propertyInfo == null)
            propertyInfo = type.GetProperty("Tag");

// rest of the code....

其中控件是System.Windows.Forms.Control.ControlCollection,并且在作为参数传递给这段代码的表单上的控件中有NumericUpDowns,我无法在控件集合中找到它们(controls = myForm.Controls),但是是其他类型的控件(updownbutton,updownedit)。问题是我想获取NumericUpDown的Tag属性,并且在使用检查表单控件的递归方法时无法获取它。

1 个答案:

答案 0 :(得分:1)

Tag propertyControl类定义。

因此,你根本不需要反思;你可以简单地写一下

object tag = control.Tag;

您的代码无效,因为控件的实际类型(例如NumericUpDown)未定义单独的Tag属性,GetProperty不搜索基类属性。


顺便说一句,在你的第一个if州,你可以简单地写

if (control is TextBox)