【发布时间】:2021-02-21 16:21:41
【问题描述】:
如何获取/设置Control(在本例中为Button)的属性?
我尝试过这种方式:
Type t = Type.GetType("System.Windows.Forms.Button");
PropertyInfo prop = t.GetType().GetProperty("Enabled");
if (null != prop && prop.CanWrite && prop.Name.Equals("button1"))
{
prop.SetValue(t, "False", null);
}
但 t 为空。这里有什么问题?
【问题讨论】:
-
t.GetProperty()而不是t.GetType().GetProperty() -
prop.Name将是Enabled,而不是button1 -
但是 t 对象一直为空。
-
类型名称应该是程序集限定,类似于
"System.Windows.Forms.Button, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089";请注意,版本和公钥可能会有所不同 `
标签: c# winforms reflection properties