通过字符串变量访问控件

string t = "textbox1";
TextBox tb = (TextBox)this.GetType().GetField(t, System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Instance | System.Reflection.BindingFlags.IgnoreCase).GetValue(this);
tb.text="成功";

通过字符串变量访问对象属性值

SRE实战 互联网时代守护先锋,助力企业售后服务体系运筹帷幄!一键直达领取阿里云限量特价优惠。
//获取x的类型
Type t = x.GetType();
//通过变量"type"访问x的类型的属性变量typeStr的属性信息(propertyInfo)
System.Reflection.PropertyInfo propertyInfo = t.GetProperty(typeStr);
//通过属性信息,使用对象实例赋值。
propertyInfo.SetValue(x, "成功", null);
                

 

扫码关注我们
微信号:SRE实战
拒绝背锅 运筹帷幄