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属性,并且在使用检查表单控件的递归方法时无法获取它。     
已邀请:
Tag
属性由
Control
类定义。 因此,你根本不需要反思;你可以简单地写
object tag = control.Tag;
您的代码无效,因为控件的实际类型(例如,
NumericUpDown
)未定义单独的
Tag
属性,而
GetProperty
不搜索基类属性。 顺便说一句,在你的第一个
if
状态中,你可以简单地写
if (control is TextBox)
    

要回复问题请先登录注册