加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – PropertyGrid.BrowsableAttributes有什么兴趣?

发布时间:2020-12-15 23:34:51 所属栏目:百科 来源:网络整理
导读:NB : This question is tagged C# too as it is a general question and an answer describing the use of this in C# is perfectly fine to me. 我最近在.Net Framework中一直在探索PropertyGrid.我检查了这个属性(BrowsableAttributes),我不知道它的使用.

NB : This question is tagged C# too as it is a general question and an
answer describing the use of this in C# is perfectly fine to me.

我最近在.Net Framework中一直在探索PropertyGrid.我检查了这个属性(BrowsableAttributes),我不知道它的使用.

起初我以为这将能够循环遍历SelectedObject中的每个BrowsableAttribute,然后你就能找回原来的属性,这本来是有用的.

但不,显然所有这个属性都给你一个只包含BrowsableAttribute的AttributeCollection,都设置为True …

有人可以告诉我这种方法有什么意义吗?我甚至不明白它在.NET中是如何有用的……

Dim attributes = MyPropertyGrid.BrowsableAttributes
For Each A As Attribute In attributes
    Dim Browsable As BrowsableAttribute = CType(A,BrowsableAttribute)
    'Then how can I use this ? it's only property is Browsable (True/False)
Next

我原本试图解决一个问题,我不知道在属性网格中选择了哪个对象,但我想收集对象的数据.

我不知道对象的类型是什么,因为它来自动态加载的DLL.我只知道它是另一个派生类,我知道.但我很有兴趣备份从Property Grid获得的Object属性,以便以后能够保存和加载它们.

由于属性网格已经包含所有这些值,我认为这种属性可能是编写更多代码的重点.我不想使用反射来检查代码,而Property Grid已经这样做了.

解决方法

BrowsableAttributes财产的工作已在文件中清楚地描述:

Only properties with attributes matching the values specified are
displayed in the PropertyGrid. The default is an AttributeCollection
containing only BrowsableAttribute.Yes.

但它是如何工作的?

.NET Framework有两种机制来查找类型的元数据:

>反思API
> TypeDescriptor机制

通过类型的反射返回的元数据是不可扩展的,并且在编译类型后无法修改,而TypeDescriptor返回的元数据可以使用IExtenderProvider,ITypeDescriptorFilterService或ICustomTypeDescriptor进行更改.

例如,这是类型描述机制,它使设计人员能够添加一些设计时属性,这些属性不是对象的实际属性,如Modifier,Locked或GenerateMember.

PropertyGrid使用TypeDescriptor机制以这种方式获取属性:

var properties = TypeDescriptor.GetProperties(component,attributes);

这依赖于TypeDescriptor.GetProperties方法,您可以在文档的备注部分找到过滤规则.

上述方法中的组件是PropertyGrid的SelectedObject,属性是BrowsableAttributes属性,如文档中所述,只有属性与指定值匹配的属性才会显示在PropertyGrid中.由于默认情况下基于约定我们期望具有[Brawsable(false)]的属性不会显示在PropertyGrid中,因此该属性包含带有Yes值的Browsable属性.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读