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

 

所以,才有了下面的方法:

 

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" ]);

 

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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM