c#在WinForm和WebForm中根據控件和屬性名獲取控件屬性值

在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);
            }
        }

 

因此,纔有了下面的方法:ui

 

Control control = Controls.Find("button1", true)[0];spa

object o = control.GetType().GetProperty("PropertyName").GetValue(control, null);.net

System.Reflection.EventInfo ev = control.GetType().GetEvent("Click");線程

你這是要獲取事件吧,第二行是獲取屬性,第三行是獲取事件code

 

出處:https://blog.csdn.net/lunyesheng/article/details/52932477orm

======================================================對象

順便看到網上有 WebForm 獲取控件的,也一塊兒貼出來吧!blog

     t.Attributes.Add( "s" "abc" );
     Response.Write(t.Attributes[ "s" ]);

 

出處:https://bbs.csdn.net/topics/310129526繼承

相關文章
相關標籤/搜索