日期:2014-05-19  浏览次数:20745 次

怎么获取控件的相关属性?
PropertyDescriptorCollection   properties   =   TypeDescriptor.GetProperties(comboBox1);
foreach   (PropertyDescriptor   pd   in   properties)
{
        listBox1.Items.Add(pd.Name);
}

这样写是获取成员名称,好像不对,我只想获取有关的属性;而有关的属性是仅仅是在属性窗口中显示的属性,如果有属性为[Browsable(false)]那么就不获取这个属性名称;请问应该做,小弟初学请多多指教,谢谢!

------解决方案--------------------
获取了属性之后,应该还可以判断[Browsable(false)]

Type vType = comboBox1.GetType();
PropertyInfo[] properties = vType.GetProperties();
foreach (PropertyInfo pi in properties)
{
//判断该属性上是否定义了[Browsable]的Attribute
if ( !( Attribute.IsDefined(pi, typeof(System.ComponentModel.BrowsableAttribute))))
{
//如果没有就直接添加
checkedListBox1.Items.Add(pi.Name);
}
else
{
//如果有,获取该Attribute然后读取其Browsable属性值,如果为True则添加
System.ComponentModel.BrowsableAttribute BrowserObj = (System.ComponentModel.BrowsableAttribute)Attribute.GetCustomAttribute(pi, typeof(System.ComponentModel.BrowsableAttribute));
if (BrowserObj.Browsable)
checkedListBox1.Items.Add(pi.Name);

}
}