【问题标题】:ICustomTypeDescriptor: When will GetProperties(Attribute[]) be called?ICustomTypeDescriptor:什么时候调用 GetProperties(Attribute[])?
【发布时间】:2013-07-25 14:05:34
【问题描述】:

通过阅读 ICustomTypeDescriptor http://msdn.microsoft.com/de-de/library/system.componentmodel.icustomtypedescriptor.aspx 的 MSDN 文章 我无法找到对差异的可靠解释 GetProperties()GetProperties(Attribute[])

第二种方法使用什么属性,描述符如何决定它是否调用GetProperties 有或没有Attribute 数组。

(我已经移植了一些代码和用于调用GetProperties(Attributes[]) 的旧代码中的属性网格,但是新代码只调用了没有属性的GetProperties,我看不出是什么影响了这一点)

【问题讨论】:

标签: c#


【解决方案1】:

我无法找到一个可靠的解释来解释 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@ 属性。

【讨论】:

    猜你喜欢
    • 2018-02-16
    • 2019-12-25
    • 2014-03-27
    • 1970-01-01
    • 2010-10-18
    • 2016-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多