我无法找到一个可靠的解释来解释
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 Studio 中使用的 PropertyGrid 控件使用此机制将所选对象上的属性分组到类别中,例如,当您在设计画布上选择一个 TextBox 时,该 TextBox 的属性将显示在 PropertyGrid 中并进行分类进入布局、字体、杂项等...
这是通过使用 Category 属性注释 TextBox 类中的这些属性来实现的,然后 TypeDescriptor 在 TextBox 类上调用 GetProperties(Attributes [] attributes) 并在数组中传递 Category 并且 TextBox 返回具有 @ 的所有属性987654336@ 属性。