通过阅读ICustomTypeDescriptor http://msdn.microsoft.com/de-de/library/system.componentmodel.icustomtypedescriptor.aspx的MSDN文章,我无法确切地解释GetProperties()和GetProperties(Attribute[])的区别。
第二个方法使用哪些属性,描述符是如何决定是否使用Attribute数组调用Attribute的。
(我已经在调用GetProperties(Attributes[])的旧代码中移植了一些代码和属性网格,但是新代码只调用没有属性的GetProperties,我不知道是什么影响了这一点)
发布于 2013-07-25 15:05:28
我无法确切地解释
GetProperties()和GetProperties(Attribute[])的区别。
主要区别在于,GetProperties()返回在实现ICustomTypeDescriptor的类型上定义的所有属性,而GetProperites(Attributes [] attributes)返回属性列表,这些属性至少具有Attribute[] attributes参数中的一个属性。
检查这个示例实现,它使用GetProperties()获取属性列表,然后根据Attributes[]数组过滤它。
public override PropertyDescriptorCollection GetProperties(Attribute[] attributes)
{
List<PropertyDescriptor> descriptors = new List<PropertyDescriptor>();
foreach (PropertyDescriptor descriptor in this.GetProperties())
{
bool include = false;
foreach (Attribute searchAttribute in attributes)
{
if (descriptor.Attributes.Contains(searchAttribute))
{
include = true;
break;
}
}
if (include)
{
descriptors.Add(descriptor);
}
}
return new PropertyDescriptorCollection(descriptors.ToArray());
}
}第二个方法使用哪些属性,以及描述符是如何决定是否使用属性数组调用GetProperties的。
所使用的属性由客户端代码选择,客户端代码使用TypeDesciptor获取属性列表。
例如,在visual中使用的PropertyGrid控件使用此机制将所选对象的属性分组为类别,当您在设计画布上选择一个TextBox时,该TextBox的属性将显示在PropertyGrid中,并被分类为布局、字体、杂项等.
这是通过用TextBox属性注释TextBox类中的这些属性来实现的,然后TypeDescriptor在数组中的TextBox类上调用GetProperties(Attributes [] attributes),然后TextBox返回在它们上具有Category属性的所有属性。
https://stackoverflow.com/questions/17860303
复制相似问题