当我这样做的时候:
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....其中controls是System.Windows.Forms.Control.ControlCollection,在作为参数传递给这段代码的窗体上的控件中有NumericUpDowns,我在controls集合(controls=myForm.Controls)中找不到它们,但有其他类型的控件(updownbutton,updownedit)。问题是,我想要获取NumericUpDown的Tag属性,但在使用检查表单控件的递归方法时却无法获取。
发布于 2010-07-19 21:18:26
Tag property由Control类定义。
因此,您根本不需要反射;您可以简单地编写
object tag = control.Tag;您的代码不能正常工作,因为控件的实际类型(例如NumericUpDown)没有定义单独的Tag属性,并且GetProperty不搜索基类属性。
顺便说一下,在您的第一个if声明中,您可以简单地编写
if (control is TextBox)https://stackoverflow.com/questions/3281308
复制相似问题