在WinForm中開發的過程中,所有繼承了Control控件,在使用多線程的時候,就需要通過一個通用的方法來設置界面上的控件的屬性。
delegate void DelegateSetControl(Control ctrObj, string attrName, object attrVal); public static void SetControlValue(Control ctrObj, string attrName, object attrVal) { if (ctrObj.InvokeRequired) { DelegateSetControl delegateSetButtion = new DelegateSetControl(SetControlValue);//實例化委托對象 ctrObj.Invoke(delegateSetButtion, ctrObj, attrName, attrVal); } else { ctrObj.GetType().GetProperty(attrName).SetValue(ctrObj, attrVal); } }
所以,才有了下面的方法:
Control control = Controls.Find("button1", true)[0];
object o = control.GetType().GetProperty("PropertyName").GetValue(control, null);
System.Reflection.EventInfo ev = control.GetType().GetEvent("Click");
你這是要獲取事件吧,第二行是獲取屬性,第三行是獲取事件
出處:https://blog.csdn.net/lunyesheng/article/details/52932477
======================================================
順便看到網上有 WebForm 獲取控件的,也一起貼出來吧!
t.Attributes.Add(
"s"
,
"abc"
);
Response.Write(t.Attributes[
"s"
]);