首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ICustomTypeDescriptor:什么时候叫GetProperties(Attribute[])?

ICustomTypeDescriptor:什么时候叫GetProperties(Attribute[])?
EN

Stack Overflow用户
提问于 2013-07-25 14:05:34
回答 1查看 1.5K关注 0票数 0

通过阅读ICustomTypeDescriptor http://msdn.microsoft.com/de-de/library/system.componentmodel.icustomtypedescriptor.aspx的MSDN文章,我无法确切地解释GetProperties()GetProperties(Attribute[])的区别。

第二个方法使用哪些属性,描述符是如何决定是否使用Attribute数组调用Attribute的。

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

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-07-25 15:05:28

我无法确切地解释GetProperties()GetProperties(Attribute[])的区别。

主要区别在于,GetProperties()返回在实现ICustomTypeDescriptor的类型上定义的所有属性,而GetProperites(Attributes [] attributes)返回属性列表,这些属性至少具有Attribute[] attributes参数中的一个属性。

检查这个示例实现,它使用GetProperties()获取属性列表,然后根据Attributes[]数组过滤它。

代码语言:javascript
复制
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属性的所有属性。

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/17860303

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档