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继承