由于线程安全问题,在多线程编程下更改一个控件的属性时,往往需要用托管来更改属性的值.下面是一个通用的托管,用反射来对属性进行赋值.

public delegate void SetValueCallBack(Control control, string property, object value);
public static void SetValue(Control control, string property, object value)
...{
     if (control.InvokeRequired)
         control.Invoke(new SetValueCallBack(SetValue), new object[] ...{ control, property, value });
     else
     ...{
         PropertyInfo p = control.GetType().GetProperty(property);
         p.SetValue(control, Common.Data.To(value, p.PropertyType), null);
         Application.DoEvents();
     }
}

 this.Invoke((EventHandler)delegate{this.Text = "test";});

相关文章:

  • 2021-10-19
  • 2022-12-23
  • 2021-12-13
  • 2021-11-29
  • 2022-01-18
  • 2021-09-26
  • 2021-10-07
猜你喜欢
  • 2022-12-23
  • 2022-12-23
  • 2021-11-21
  • 2022-12-23
相关资源
相似解决方案